I am trying to node_save($node) in different database. Second DB is clone of first DB.

its the part of Archive old content data in different database

I am trying with following code

  $node = node_load($nid);

But its not working. Is anything i am doing wrong ?

i want to use node_save because in my node, number of fields, field_collection, entity_refrences are available.

  • Have you try to read the data from that new DB, does it work? – GwenM Feb 3 '16 at 9:54
  • hi @Namari : Yes, i am able to read the data from my newDB. – visabhishek Feb 3 '16 at 10:32
  • One thing i also noticed, i am able to update node. Means same node if exist in both db and i have updated in default db then its updating in 'archive_db' using above code. But its not working if node is not present in ''archive_db'. – visabhishek Feb 3 '16 at 11:32
  • Try this: unset($node->nid); $node->is_new = true; have you tried entity wrapper? – GwenM Feb 3 '16 at 13:07
  • Thanks @Namari, Now its saving the node , and adding field_collections and entity_reference also but data is not saving :( for field_collections. – visabhishek Feb 3 '16 at 14:11

Try this, it was working for me:

$nid = '2841';
$item_ids = array();
$node = entity_load('node', array($nid), array(), true);
foreach ($node[$nid]->field_COLLECTION_NAME[LANGUAGE_NONE] as $key => $value) {
    $item_ids[] = $value['value'];

//load the field collections
$fc_item = entity_load('field_collection_item', $item_ids);

//change db

$clone = new stdClass();
$clone = clone $node[$nid];
$clone->is_new = true;

//save node so we can use the new nid to link the field collection
entity_save('node', $clone);
//gives you the nid of the new node created

//save the field collections
foreach ($fc_item as $key => $value) {
    //unset values
    $value->is_new = true;
    $value->revision_id = NULL; 
    $value->item_id = NULL;
    //link it to a node
    $value->setHostEntity('node', $clone);

dsm(entity_load('node', array($clone->nid), array(), true));
//go back to the default DB
  • Are you used field_collections with unlimited value in you entity ? My problem with Field_collections and Paragraph entity values are not saving; – visabhishek Feb 5 '16 at 12:34
  • Is your field collection in the node_load? – GwenM Feb 5 '16 at 13:02
  • I've edited, have a look. – GwenM Feb 5 '16 at 13:12
  • same code i already tried , but no luck :( Its not saving the value – visabhishek Feb 5 '16 at 13:35
  • I've edited, this should work. I had a weird behaviors giving me empty field collection before the one with the data, it disappears once the node is saved though. – GwenM Feb 5 '16 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.