node_save($node) don't save uid

upupax - January 7, 2009 - 10:01

I'm trying to submit a node throw my module with this

$edit = array();
$edit['type'] = 'my_content';

global $user;
$uid = $user->uid;
$edit['uid'] = 1;
$edit['promote'] = 0;
$edit['comment'] = 0;
$edit['status'] = 1;
$edit['title'] = 'Author '.$uid;

//cck field
$edit['field_field1'][0]['nid'] = $nid;

node_invoke_nodeapi($edit, 'my_content');

node_validate($edit);
if ($errors = form_get_errors()) {
//print_r($errors);
}

$node = node_submit($edit);
node_save($node);

The node is generated correctly, the title is set as 'Author 1' (and it's ok cause i'm superadministrator), but the node author is set to anonymous even if I've set it to 1.
What am I doing wrong?
Doesn't node_save submit even author uid?

They're all supposed to be

pobster - January 7, 2009 - 11:10

They're all supposed to be objects as per the api instructions; http://api.drupal.org/api/function/node_submit/6

Pobster

I guess there's no

upupax - January 7, 2009 - 12:02

I guess there's no error..
but if I have a look to the db, the uid is set to 0 even if I put 1.

...Well I could write the

pobster - January 7, 2009 - 15:39

...Well I could write the exact same thing I wrote above as you've obviously not read it the first time... But instead, I'll just point you to an example.

http://acquia.com/blog/migrating-drupal-way-part-i-creating-node

OBJECT not ARRAY.

If you're properly having problems with understanding what I'm talking about then don't set the uid yourself use node_object_prepare instead (http://api.drupal.org/api/function/node_object_prepare/6).

Pobster

This seems really

upupax - January 7, 2009 - 15:51

This seems really useful...
Thank you!

node_submit is the enemy

Ralla - January 16, 2009 - 11:37

node_submit(); overrides the uid if you have administrator rights to nodes. So this is what you want:

This would work:

<?php
$edit
= array();
global
$user;
$account = user_load(array('uid' => $user->uid));

$edit->uid = $account->uid;
$edit->type = 'my_content_type';
$edit->promote = 0;
$edit->comment = 0;
$edit->status = 1;
$edit->title = 'Author ' . $account->uid;

//cck fields
$edit->field_field1[0]['value'] = $nid;

// Because we are not using node_submit anymore, we have to set some extra values
$edit->body = 'Some content...'
$edit->teaser = 'A teaser...';
$edit->created = time();
$edit->validated = TRUE;

node_save($node);
?>

This did the trick... Thanks

upupax - January 16, 2009 - 14:08

This did the trick...
Thanks a lot!

You're most welcome :)

Ralla - January 23, 2009 - 09:49

You're most welcome :)

 
 

Drupal is a registered trademark of Dries Buytaert.