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.
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | entityreference_wrapper_property_set-2143171-4.patch | 3.21 KB | rosk0 |
| #1 | 2143171-entityreference-wrapper-property-set.patch | 2.59 KB | wodenx |
Comments
Comment #1
wodenx commentedComment #2
wodenx commentedMay also be an issue with UUID - see #2123779: Entity loaded with entity_uuid_load cannot be safely wrapped if entity references are used
Comment #3
Offlein commentedHi, 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).Comment #4
rosk0I've tested patch from #1 and it doesn't work.
I've took its idea and created new one that works.
Comment #5
mvantuch commentedI've just came around same issue while implementing a custom exportable entity.
Patch #4 worked well for me.
Comment #6
cyb_tachyon commented#4 Tested & working fine with custom exportable entities.
Some code review:
Perhaps something like
These comments are too long (80+), break the last word out onto the next line:
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.
Comment #7
nielsonm commented#4 Looks good - marking as RTBC