Hi everybody,

i have a little block with some very basic PHP code.

Now, when user klicks the button, i want to update a cck field (a user reference) in another node.

I found the field in the db and it looks like "vid nid delta field_name_uid" but i wonder if adding something here is realy a good idea ? I´m not even confident what "vid" and "delta" would be etc..

So is there a drupal function "update user reference CCK field from node X enter UID Y" ?

I´m also interested to know if there are ways to update other CCK field types with drupal functions.. ?
Or maybe can this be done with a URL commant like /node/1/edit/just this filed/with this value ?

Thx in advance for your time, John

Comments

drclaw’s picture

If you know the nid of the node you wish to update, you can load the node, then change the value of the field then save it:

$nid = 7; // Just a random node ID for examples-sake
$uid = 1; // A random user ID for examples-sake
$node = node_load($nid);
// Drupal 6
$node->field_my_field[0]['uid'] = $uid;
// Drupal 7 -- You need to know what language the field value was saved in i.e. 'und' for undefined, 'en' for english etc.
$node->field_my_field['und'][0]['uid'] = $uid;
node_save($node);

If you're not sure how your field array is setup in the node, install and enable the Devel module. Then, go to a node page and click the 'devel' link in the local tasks area (should be next to 'view' and 'edit'). Expand the little bar that says something like ... (Object) stdClass and find your field and expand it however many times you need. (Sorry if you already know how to do that, just wanted to be thorough. =P)

bacon’s picture

Very nice. You beat me by a few minutes.

fast women, pretty cars, sexy computers
https://coderwall.com/algorgeous/

drclaw’s picture

That's funny, whenever I'm typing up an answer to someone's question I always wonder if someone will have beaten me to it when I hit save. I guess I won this one. =P

bacon’s picture

You can take a look at this page
http://www.unleashed-technologies.com/blog/2010/07/16/drupal-6-inserting...
that shows you how to edit CCK Fields/Node in PHP.

It actually doesn't give an example of updating a User Reference field but it is similar to this:

// Load up your node
$node = node_load($nid_to_change);
// Update the user reference field
$node->my_user_reference_field[0]['uid'] = $user_ref_id;
// Save the updated node
node_save($node);

If you've already got some PHP in your block, then it shouldn't be too hard to add this code to change the user reference in the desired node.

fast women, pretty cars, sexy computers
https://coderwall.com/algorgeous/

john_the_noob’s picture

Wow, you guys are very helpful. The link is good too !
I really try to do this "the drupal way". (D6 by the way)

2 questions:

1)If i want to add another reference, should i count all values in the array and insert the new one @ "all uids yet+1" ?
2)If i want to delete one ?

Thx a lot again, John

drclaw’s picture

1) Yep.

$count = count($node->field_my_field);
// You don't need to add 1 to the count because the field index starts at 0
$node->field_my_field[$count]['uid'] = $uid;

2) For deleting you can just unset a particular item.

unset($node->field_my_field[0]);
john_the_noob’s picture

Thank you very much.

john_the_noob’s picture

fun how much easier things can be. this is what i did to count rows:

$countrows = db_result(db_query("SELECT COUNT(*) FROM {my_field} where nid ='$current_nid' "));

bacon’s picture

To avoid SQL injection you db_query should look like this:


$countrows = db_result(db_query("SELECT COUNT(*) FROM {my_field} WHERE nid = %d", $current_nid));

fast women, pretty cars, sexy computers
https://coderwall.com/algorgeous/

zeta1600’s picture

Bacon, I have a cck user reference field on a content type. I have to delete user "John Doe", user 26, who is referenced and change it to another user "Jane Doe", user 84.

Is your code what I should use to do this? What do I do with this code? Is it a function?

Your step by step help would be greatly appreciated.

zeta1600’s picture

So I just learned that this is to be made into a module. Can you help me create the php for this?

I have to delete user "John Doe", user 26, who is referenced and change it to another user "Jane Doe", user 84. My field is called field_manager.

bacon’s picture

Is this a one time process you are trying to run? If so, you might not need to create a module.

It may be easier to just write a small php script file and use drush to execute it.


$nid_to_change = 9999;
$user_ref_id = 84;

// Load up your node
$node = node_load($nid_to_change);
// Update the user reference field
$node->field_field_manager[0]['uid'] = $user_ref_id;
// Save the updated node
node_save($node);

Place the above code in a file named fix_user_reference.php (or whatever you want to name it).

Execute your script with drush.
drush php-script fix_user_reference

If it's just a one-off change you can do like above. For multiple nodes, put your node ids in an array and loop through them.

If you need to do it for a whole bunch of nodes, then it may be easier to write and SQL statement that will update the table changing the uid to 84 anywhere where the uid is 26.

fast women, pretty cars, sexy computers
https://coderwall.com/algorgeous/

john_the_noob’s picture

Side note for anybody who has the same problem: The code above works fine for normal fields, but remember references do have their own table !

(count() will not work)

I found another solution + some cashing problems but will make another thread so others who search for the same thing will be able to find it later: http://drupal.org/node/1127868