By rupat on
Hello Drupal friends !
1. I´ve written a module which gets an extra node type
2. there is a cron job in this module, which imports from external sources data and creates new nodes of this type automaticly if new data has arrived
For now i have written a little function, that makes the sql queries for adding the right entries in the right tables
looks like:
db_query("INSERT INTO {node} (nid, vid, title, type, uid, status, created, changed, comment, promote, moderate, sticky) VALUES (%d, %d, '%s', '%s', %d, %d, %d, %d, %d, %d, %d, %d)", $l_sNId, $l_sVId, $l_sTitle, 'mixl', 85, 1, $l_date, time(), 2, 0, 0, 0);
db_query("INSERT INTO {node_revisions} (nid, vid, title, timestamp, uid, format) VALUES (%d, %d, '%s', %d, %d, %d)", $l_sNId, $l_sVId, $l_sTitle, $l_date, 85, 0);
db_query("INSERT INTO {mixl}_nodes (nid, url, import_url, parked, pid) VALUES (%d, '%s', '%s', %d, %d)", $l_sNId, $p_sLink, $p_sLink, 0, $p_iPId);
db_query("INSERT INTO node_comment_statistics (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d,%d,%d,%d,%d)", $l_sNId, $l_date, NULL, 85, 0);
db_query("DELETE FROM {node_access} WHERE nid=%d AND (realm = 'nodeaccess_uid' or realm = 'nodeaccess_rid')", $l_sNId);
db_query("INSERT INTO {node_access} (gid, realm, grant_view, grant_update, grant_delete, nid) VALUES (1, 'nodeaccess_rid', 0, 0, 0, %d)", $l_sNId);
db_query("INSERT INTO {node_access} (gid, realm, grant_view, grant_update, grant_delete, nid) VALUES (2, 'nodeaccess_rid', 0, 0, 0, %d)", $l_sNId);
db_query("INSERT INTO {node_access} (gid, realm, grant_view, grant_update, grant_delete, nid) VALUES (6, 'nodeaccess_rid', 1, 0, 0, %d)", $l_sNId);
db_query("INSERT INTO {node_access} (gid, realm, grant_view, grant_update, grant_delete, nid) VALUES (7, 'nodeaccess_rid', 1, 1, 1, %d)", $l_sNId);
db_query("INSERT INTO {node_access} (gid, realm, grant_view, grant_update, grant_delete, nid) VALUES (8, 'nodeaccess_rid', 1, 1, 1, %d)", $l_sNId);
db_query("INSERT INTO {node_access} (gid, realm, grant_view, grant_update, grant_delete, nid) VALUES (9, 'nodeaccess_rid', 1, 0, 0, %d)", $l_sNId);
db_query("DELETE FROM {term_node} WHERE nid=%d", $l_sNId);
db_query("INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)", $l_sNId, $l_sTId);
But this seems to be not really the best solution i think. I often use for editing the nodes the simple function node_save. Is there an equivalent to create a node of my type ?
Any Hints are welcome !
Comments
Of course
It's node_save.
If you pass node_save a node object with no nid, it will create a new record.
http://api.drupal.org/api/function/node_save/6
Read the source code.
Much better, safer, future-proof than jamming the tables yourself.
ah thx
thx for your help !