Drupal - How to update a CCK NodeReference field programmatically?
Asked Answered
C

5

7

I'm trying to create a node (B type) & assign it to a A type node's CCK nodereference field using node_save() method.

$node_type_A = node_load($some_nid);
$node_type_A->field_type_B_node_ref[]['nid'] = $node_type_B_nid;

$node_type_A = node_submit($node_type_A);
node_save($node_type_A);

As the result, a new B type node will be created, but no reference will be assigned to the A type node. any help would be appreciated.

Crackleware answered 27/4, 2010 at 17:42 Comment(0)
C
6

GApple is right, the format is correct, but there are couple of things that you might want to care about.

Delta Value
First you need to know the delta value of the latest node reference attached to $node_type_A, the delta is actually a partial index, when combined with vid field of the $node_type_A, they become the index for node reference table in the database. In other words, its a count for $node_type_B which are referenced in $node_type_A, ok?

GApple is right again, you have to exactly say where to add the new reference. When you got that delta value you can exactly say where to append (delta+1) the new reference. Here it is:

function get_current_delta($node_vid){
    return db_result(db_query("SELECT delta FROM {content_field_type_A_node_ref}
                               WHERE vid = '%d'
                               ORDER BY delta DESC
                               LIMIT 1", $node_vid));
}

Adding the new reference
We got delta! so we can attach the new $node_type_B node to our $node_type_A node:

// Loading type_A node.
$node_type_A = node_load($some_nid);

// Getting current delta value.
$current_delta = get_current_delta($node_type_A->vid);

// "Appending" a node reference based on delta. 
$node_type_A->field_type_B_node_ref += array($current_delta + 1 => array('nid' => $node_type_B_nid));

Resaving the updated node
Optionally call node_submit() to populate some essential fields in the node object and save it by utilizing node_save(). After all, you need to call content_insert() to make the node completely saved asidelong with its CCK fields:

// Resaving the updated node.
$node_type_A = node_submit($node_type_A);
node_save($node_type_A);
content_insert($node_type_A);

Flushing the content cache
Probably the most important part, this was killin' me for couple of days. CCK has a cache table in the database called cache_content (take a look at its structure), after resaving the updated node, you will notice that nothing has changed in the $node_type_A theme output even though that the tables are updated. We have to remove a record from that content cache table, this will force Drupal to show the latest snapshot of the data. You can define the following as a function:

db_query("DELETE FROM {cache_content} WHERE cid = '%s'", 'content:' . $node_type_A->nid . ':' . $node_type_A->vid);

Hope it helps ;)

Cayuse answered 5/5, 2010 at 14:52 Comment(1)
...and this is why I hate programming with the drupal API. That solution is ridiculous. Thanks heaps!Experience
B
4

I just checked one of my own modules that does something similar for the object format, and $node_type_A->field_type_B_node_ref[]['nid'] should be correct.

One thing to check for is that when you load the node, CCK may pre-populate the node reference array with an empty value. If you have configured the field to only allow one value, by using the array append operator (field_type_B_node_ref[]) it will create a second entry that will be ignored (field_type_B_node_ref[1]), instead of overwriting the existing value (field_type_B_node_ref[0]). Try explicitly specifying the array key if possible.

Bisector answered 27/4, 2010 at 19:46 Comment(1)
I've just checked that & it was set to unlimit, Also I tried to specify the exact index value of the array, still nothing :(Crackleware
P
3

Great post, but one correction: don't flush cache entries by manually querying the DB. In the event someone is using memcache or any other external cache it's going to fail.

cache_clear_all() is your friend for clearing.

Suggested code, direct from the CCK module:

cache_clear_all('content:'. $node_type_A->nid .':'. $node_type_A->vid, content_cache_tablename());
Proponent answered 31/3, 2012 at 4:54 Comment(0)
S
0

I show CCK storing node references as $node->field_node_reference[0]['items'][0]['nid'], not $node->field_node_reference[0]['nid']. Have you tried mimicking that?

Shadshadberry answered 27/4, 2010 at 17:51 Comment(5)
gonna try that, and I'll post back the results.Crackleware
Hmm. I don't see anything wrong with the code, then. Are you sure $node_type_B_nid is properly populated?Shadshadberry
yes, I'm sure. there's nothing buggy about that $node_type_B_nid. Additionally I used to use $node->field_node_ref[0..n]['nid'] format while CREATING nodes & it works, but it failing now, on update, thank you anyways.Crackleware
are you using that code on a nodeapi hook? notice there are different $op for creating the node and editing it. check api.drupal.org/api/function/hook_nodeapi/6 for more info.Schaeffer
content cache was the problem.Crackleware
C
0

"Flushing the content cache" This works for me, especially if you get a data from node_load()

Compte answered 24/5, 2010 at 1:39 Comment(1)
you can add a comment on an answer instead of answering a comment ^^Crackleware

© 2022 - 2024 — McMap. All rights reserved.