By tormu on
I'm trying to import articles from our old DB, so I create the node object, set $node->title, body and so on, taxonomies too and everything goes well.
But when I try to save the creation date of the node NOT to be current moment, it fails:
//Get the date of the article
if(!empty($result->broadcast_date))
$date = strtotime($result->broadcast_date);
$node->created = $date;
$node->modified = $date;
print date('Y-m-d', $node->created) //Prints out dates like 2003-07-12
then save the node:
$node = node_submit($node);
//The print_r below prints the object to be saved - the modified-date is correct, but created-date is blank - WHY?
print_r($node) . "<br />";
node_save($node);
//No creation date set before node_save() and this moment will be set.
Why does the node_submit blank out the creation-date? I guess it's ok just to set the creation date again after the node_submit(), but I'm interested in why this is happening..
Comments
You probaly have 'administer nodes' permissions
You probaly have 'administer nodes' permissions, add the follow line before node_submit and it should work.
It's very strange, that
It's very strange, that node_submit seems to blank out variety of elements.. I set the author ID by assigning a value for $node->uid and when the script does node_submit(), the $node->uid is all the sudden changed to 0.
This also occurs with some CCK-fields, node_submit blanks out the values and I have to add the values back after node_submit, which makes me wonder what's the node_submit for anyway, I just got the impression that it should be ran anyway first before saving..
regarding $node->uid
You need to set $node->name to a valid drupal user name if you have 'administer nodes' permissions.
As for the CCK fields that probably has to do with the way CCK works but it would requiring playing around.
those two solved
Ok, setting the $node->date and $node->name solved the uid and created, thanks for both replys :)
Which includes
I would like to make a cron that imports some variables I've parsed in to a CCK content type. Which files do I need to include for drupal functions (ie node_save) to work? And should I make a module for it, if it's a cron job?
I would make it a module and implement the cron hook
I would make it a module and implement the cron hook since this should simplify things considerably. This way the task is running as part of Drupal. There is the question of how the variables are passed to the cron job but that depends on what the variables are.