The target id for an entityreference field must be numeric. However, the default Entity API field setter passes a string id (the entity name) when setting fields to a wrapped exportable entity. Thus, if you set an entityreference field via a wrapper to an exportable entity, and then try to save the entity holding the field, you'll get a PDO Exception (invalid integer value for column target_id).

Steps to reproduce.

1. Create a rules_config entity name "rules_myconfig".
2. Add an entityreference field with a target entity type of "rules config" to any content type (call it "field_rules_config_ref").
3. Create a node of that type with nid = $nid.
4. Try to run the following code:

$wrapper = entity_metadata_wrapper('node', $nid);
$wrapped_config = entity_metadata_wrapper('rules_config', 'rules_myconfig');
// Same thing happens with:
// $wrapped_config = entity_metadata_wrapper('rules_config', entity_load_single('rules_config', <numeric-id-of-the-config>));
$wrapper->field_rules_config_ref->set($wrapped_config);
$wrapper->save();

Patch to follow.

Comments

wodenx’s picture

Status: Active » Needs review
StatusFileSize
new2.59 KB
wodenx’s picture

Offlein’s picture

Hi, I don't think this is completely there yet, at least. We're having the same issue with #2184763: Entity Reference of custom entity references by wrong parameter (and you can follow the earlier investigation here #2183095: Countries entity implementation may be wrong; fails if countries are Organic Groups).

To paraphrase, the Countries module uses the column "iso2" as its name, but entity reference goes by the integer "cid" column.

Saving by entity reference autocomplete works more-or-less OK, but we used OG to make our countries entities into groups, too. When a node referencing an OG group is saved, OG goes through each field and manually calls EntityMetadataWrapper's ->set() method for each reference. Your issue for whatever reason still rears its head in this case and we get a EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityDrupalWrapper->set() (line 736 of /var/www/clean/sites/all/modules/entity/includes/entity.wrapper.inc).

rosk0’s picture

I've tested patch from #1 and it doesn't work.
I've took its idea and created new one that works.

mvantuch’s picture

I've just came around same issue while implementing a custom exportable entity.

Patch #4 worked well for me.

cyb_tachyon’s picture

#4 Tested & working fine with custom exportable entities.

Some code review:

// But use our property setter.

Perhaps something like

// Override the default property setter to use entity reference target_ids.
// Entityreference only supports numeric entity ids for fields.

These comments are too long (80+), break the last word out onto the next line:

 * of a target entity.  We need to store the numeric id, so we try to find it out
// If this entity type supports a 'name' key, then try loading the entity

It's also worth noting that custom entity controllers must extend the entityapi module's EntityAPIController if they use the name key in order to properly handle metadata wrapper setting and saving.

nielsonm’s picture

Status: Needs review » Reviewed & tested by the community

#4 Looks good - marking as RTBC