I seek assistance with properly setting the value of an entity reference field in code. I have defined an entity reference field in one of my content types. The field may refer only to one other content type. I have the following code:

$entity = entity_create('node', array('type' => 'calendar_header', 'uid' => 1));
$ch_wrapper = entity_metadata_wrapper('node', $entity);

// Insert a temporary title
$ch_wrapper->title->set('Under Construction');
$ch_wrapper->body->set(array('value' => 'Some text'));

$ch_wrapper->field_raw_calendar->set(array('target_id' => 27659, 'target_type' => 'node',));

The last line of this code throws this exception:

EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityDrupalWrapper->set() (line 735 of /var/www/sites/all/modules/contrib/entity/includes/entity.wrapper.inc).

What is the proper syntax to set the reference to node 27659 in the entity reference field field_raw_calendar, using the wrapper from entity_metadata_wrapper?

Thanks.

Comments

jacobson’s picture

I figured this out. The correct last line would be:

$ch_wrapper->field_raw_calendar->set(intval($cr_wrapper->nid->value()));

Where $ch_wrapper is a entity_metadata_wrapper to the node that has the entity reference field, and where field_raw_calendar is that entity reference field, and where $cr_wrapper is an entity_metadata_wrapper to the node to which reference should be set in field_raw_calendar.

Damien Tournoud’s picture

You want something like:

$ch_wrapper->field_raw_calendar = $cr_wrapper->value();

It might also be possible to use the wrapper directly (but I'm not 100% sure):

$ch_wrapper->field_raw_calendar = $cr_wrapper;
brandon_beeler’s picture

Version: 7.x-1.0-rc5 » 7.x-1.0

I have same issue I set a wrapper's value to fully loaded entity object. (Also tried using an entity metadata wrapper like shown in #2) I get a data truncated message when I try to save the entity.

This is a custom entity type, is it possible that this could be caused from the entity type being messed up? I have had no other issues with this entity type i've create until this point.

Thanks for any help.

brandon_beeler’s picture

Here is the error message :
PDOException: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'field_rental_reference_target_id' at row 1: INSERT INTO {field_data_field_rental_reference} (entity_type, entity_id, revision_id, bundle, delta, language, field_rental_reference_target_id) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] => commerce_line_item [:db_insert_placeholder_1] => 226 [:db_insert_placeholder_2] => 226 [:db_insert_placeholder_3] => item_rental [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => 4_1_1365431320 ) in field_sql_storage_field_storage_write() (line 448 of /Applications/MAMP/htdocs/sherrislist/modules/field/modules/field_sql_storage/field_sql_storage.module).

It looks like it is trying to insert the title in the field_rental_reference_target_id column.

brandon_beeler’s picture

Issue Corrected. In my hook_entity_info I had an entity key 'name' which was causing a conflict. I guess that was triggering entity reference to think that was my id.

MustangGB’s picture

Issue summary: View changes
Status: Active » Closed (outdated)
jomarocas’s picture

#2 working for me

following code


        $id_node = '103402';
        $resultnits = '103393';
        $raw_node = node_load($id_node);
        $resultnits = node_load($resultnits);
        $fc_wrapper = entity_metadata_wrapper('node',$raw_node);
        $cr_wrapper = entity_metadata_wrapper('node',$resultnits);
        $fc_wrapper->field_nit_relacionados = $cr_wrapper;
        $fc_wrapper->save();

cyb_tachyon’s picture

There is a patch available in this issue to address this problem: https://www.drupal.org/project/entityreference/issues/2143171