I'm trying to access the groups and nodes associated with a message through entity reference fields. When I create the message I am able to set values to the field og_group_ref and field_node_ref with the entity metadata wrapper, but when I load a message with message_load, those values in the object are empty.

Is there another way to get the values from fields added to the bundle?

My code:

<?php
  $message = message_create('create_group_post', array('arguments' => $arguments, 'uid' => $node->uid));

  $wrapper = entity_metadata_wrapper('message', $message);
  $wrapper->field_node_ref->set($node);
  $wrapper->og_group_ref->set($nids);
  $wrapper->save();
  
  dpm($message);

  $message = message_load($message->mid);

  dpm($message);
?>

The dpm output comes out like this:

... (Object) Message
type (String, 17 characters ) create_group_post
timestamp (Integer) 1342804769
arguments (Array, 3 elements)
data (Array, 0 elements)
uid (String, 1 characters ) 1
user (Object) stdClass
language (String, 2 characters ) en
field_node_ref (Array, 1 element)
og_group_ref (Array, 1 element)
mid (String, 3 characters ) 266

... (Object) Message
type (String, 17 characters ) create_group_post
timestamp (String, 10 characters ) 1342804769
arguments (Array, 3 elements)
data (Array, 0 elements)
mid (String, 3 characters ) 266
uid (String, 1 characters ) 1
language (String, 2 characters ) en
og_group_ref (Array, 0 elements)
field_message_rendered_body (Array, 0 elements)
field_message_rendered_subject (Array, 0 elements)
field_node_ref (Array, 0 elements)
rdf_mapping (Array, 0 elements)

If message_load reset the entity cache, the fields would be returned:

$message = entity_load('message', array($message->mid), array(), TRUE);

Comments

amitaibu’s picture

Are you doing this in hook_node_presave()? If so, then node doesn't have an NID yet.

amitaibu’s picture

Issue summary: View changes

Updated issue summary.

arosboro’s picture

@amitabu: I'm using hook_node_insert(). I've worked around the issue by calling entity_load directly and setting $reset to TRUE. I had to do that with node_load too to access the summary of the node in a callback. After I bypass the cache everything is available.

arosboro’s picture

Issue summary: View changes

typo

bluegeek9’s picture

Status: Active » Closed (outdated)