0

In Drupal 7, for some reason, i am storing some serialize($node) objects into Database. So these data in the database (in text column) are storing like:

O:8:"stdClass":47:{s:3:"vid";s:5:"78629";s:3:"uid";s:3:"523";s:5:"title";s:12:"Sync Node #6";s:6:"status";i:1;s:3:"nid";s:5:"77429";s:4:"type";s:4:"page";s:8:"language"; ..... etc
  • Now how can i transform these data back into Drupal $node Object please?
  • Then how can i node_save($node) (as new node) upon them? (What tricks will i need?)

Any idea please?

3 Answers 3

3

Now how can i transform these data back into Drupal $node Object please?

As said you can use the following code,

$node = unserialize($data);

Then how can i node_save($node) (as new node) upon them? (What tricks will i need?)

You can null the nid, vid and use node_save to save it as a new node like this:

$node->nid = NULL;
$node->vid = NULL;
node_save($node);
1

Call unserialize() on the serialized data.

4
  • Hmmm.. Then how please? Oct 15, 2012 at 6:48
  • Like so: $node = unserialize($data); where $data is the string that is stored in the database.
    – Oswald
    Oct 15, 2012 at 6:50
  • But, i mean .. i can NOT node_save($node) it :/ (Then how can i node_save($node) (as new node) upon them? (What tricks will i need?)) Oct 15, 2012 at 6:55
  • The node already has a nid and a vid. Upon saving, node 5 is updated. Call unset($node->nid); to remove the nid before saving it to create a new node.
    – Oswald
    Oct 15, 2012 at 7:02
0

I couldn't get unserialize() to work, it always returns 'false'. So, my solution was to:

$nodearray = drupal_json_decode($response->data);

$node = new stdClass();
foreach ($nodearray as $key => $value)
{
    $node->$key = $value;
}

unset($node->nid);
unset($node->vid);
unset($node->path); 

It's messy, but it ends up creating a new node object that can then be saved using node_save($node).

Frankly, this is messy, I would much prefer to have been able to use something like unserialize(). So if anyone has a hint as to why unserialize() is returning false for me, then please do let me know!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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