By john_the_noob on
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
If you know the nid of the
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:
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)
Very nice. You beat me by a
Very nice. You beat me by a few minutes.
fast women, pretty cars, sexy computers
https://coderwall.com/algorgeous/
That's funny, whenever I'm
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
Here's a good start
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:
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/
Wow, you guys are very
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
1) Yep. <?php$count =
1) Yep.
2) For deleting you can just unset a particular item.
Thank you very much.
Thank you very much.
fun how much easier things
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' "));
To avoid SQL injection you
To avoid SQL injection you db_query should look like this:
fast women, pretty cars, sexy computers
https://coderwall.com/algorgeous/
I have a cck user reference
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.
So I just learned that this
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.
Is this a one time process
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.
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_referenceIf 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/
cashe problems
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