Hi,

I was wondering what would be the best way to go about creating a node from within the cron hook of the same module.

Say you wrote a node that stores RSS from different feeds. In a cron job, I would like that RSS stuff to be inserted in the node.

The first thing that came to my mind was just to use plain "INSERT INTO ..." sql, but then I am lost as to how to get the "next" node id value. After that I thought about using the implemented hook: _insert() but then I was lost as to what kind of parameter does it except and how to go about building a $node object.

Any help would be highly appreciated. I would like to keep my module code as close to drupal standards as possible, and this light the drupal handbooks have been invaluable!

thanks!

Comments

rjung’s picture

The first thing that came to my mind was just to use plain "INSERT INTO ..." sql, but then I am lost as to how to get the "next" node id value. After that I thought about using the implemented hook: _insert() but then I was lost as to what kind of parameter does it except and how to go about building a $node object.

As a blind guess, I'd try the node_save() function -- you just populate a $node with the different attributes as array elements ($node['title'] = 'My title', etc.), then call node_save() and let it do its magic. According to the code, if you leave the node ID (nid) unset, the function will treat it as a new node and fetch the next ID for you. Figuring out what attributes you need to set is probably a matter of just examining the {node} table and then cribbing from that. ;-)

Note that I haven't tried this myself, but offhand I don't see why it won't work.

--R.J.
http://www.electric-escape.net/

--R.J.

apatel’s picture

Nice, thanks for that. I searched for node_save() and found tips and full working code as to how to go about it.

thanks again.

bilgehan’s picture

I'm also interested in this function but I couldn't find anything searching.

reformatt’s picture

Hi everyone,

I got a very strange problem.

Scenario:
I am trying to run cron.php which has some _cron hooks that preform node_save() 's.

Problem:
The cron.php works great from the browser but when I try to run it from shell like(bash) while using GET or curl; everything works up to the node_save() it seems like it can not insert any records but it does read. This is very strange and I wonder if anybody has encountered anything like this before.

Goal:
What I am trying to do is to setup a crontab that runs every 5 minutes or so.

I will be grateful for any advice.

william_frane’s picture

Reformatt, do you get any errors (as output) or is it just not creating/updating the node? I'm currently experiencing a similar problem (i.e. node_save calls won't insert content when cron.php is run from the shell) and getting the error "This content has been modified by another user, changes cannot be saved." However, this is only occurring after the node in question has been modified earlier in the same cron run, so my node_save calls do work the first time; it's just when I try to load, edit and save the same node again that it's not saved. Is this the same problem you're experiencing?

If your node_save calls aren't working at all, for the record, here's how I've been saving nodes:

$new_node->title = "Title.";
$new_node->body = "Some content.";
//etc etc

// Save the node.
node_validate($new_node);
if ($errors = form_get_errors()){
	print_r($errors);
}
$course_node = node_submit($new_node);
node_save($course_node);

Like I said, that works for me, but loading the same node again later in the same cron run (via node_load) and trying to save it doesn't.

mtbosworth’s picture

I am having the same error trying to "edit" the node via node_save() which is running during cron. For me, it doesn't have to be from shell. If I run cron from admin menu it doesn't work either. I even tried to run db_query() to update the particular table (in my case I'm updating CCK field on a custom content type) with NO success. I agree that it seems like db writes are not working in general during cron. Weird.

Also, note that I have taken my code and run it in the Devel modules Execute PHP block form and it works, but during cron it does not.

mtbosworth’s picture

Trying using :

$node = node_load($nid, NULL, TRUE);

instead of:

$node = node_load($nid);