Hi,
When i migrate a bunch of nodes that optionally reference eachother, all goes well until i migrate nodes that *don't* have the reference and try to index them with Search API. In my Migration, i have defined a fieldMapping as such:
$ref_ca_occ_mapping = $this->addFieldMapping('field_ca_occ_ref_ca_entity', 'ref_ca_entity');
$ref_ca_occ_mapping->sourceMigration(array($source));
$this->dependencies = $this->dependencies + array($source);
Since the reference is optional, sometimes the Source will not specify the 'ref_ca_entity' field. I have tried all kinds of things: feeding it an empty array, passing NULL or FALSE, removing the field etc, but for some reason I can't get it to work. The reference field on the imported node always looks like this:
field_ca_object_ref_ca_entity
und (Array, 1 element)
0 (Array, 1 element)
target_id (NULL)
The target_id => NULL causes all kinds of EntityMalformedExceptions, hinders indexing by Solr and causes this kind of warnings:
Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load()
The only way i got around this is by patching the MigrateEntityReferenceFieldHandler. This is a minor patch which simply avoids adding NULL as a target_id. I'm sorry if this is not the right place to do this, and i am open to suggestions. However, i think this patch can't do any harm and isn't obtrusive at all, so i'd like to get it in.
Patch follows in next post
Comment | File | Size | Author |
---|---|---|---|
#5 | 1569046-migrate-handle-null.patch | 1.36 KB | Damien Tournoud |
#1 | entityreference-migrate_empty_reference-1569046-1.patch | 614 bytes | thimothoeye |
Comments
Comment #1
thimothoeye CreditAttribution: thimothoeye commentedComment #2
thimothoeye CreditAttribution: thimothoeye commentedForget to set this to needs review...
Comment #3
Damien Tournoud CreditAttribution: Damien Tournoud commentedThere are a couple of related issues in there.
One of them is that Entity Reference defines the
target_id
column as nullable by default. I just opened an issue to consider changing this: #1569144: Consider making target_id NOT NULL.The second issue is that Migrate doesn't seem to deal well with non-defined values. During the mapping, Migrate does:
Which means that if there is a value in the data row, even NULL, it will be considered.
I'm not sure that modifying the Field handler of Entity Reference is the way to go. I looked at the fields implemented in Migrate, and most of them do *not* filter out empty values (except the node reference and user reference ones, which means that you cannot reference the anonymous user in an user reference field!).
I'm moving this to the Migrate queue for advice on how to proceed.
Comment #4
mikeryanI think the patch is the right approach (although it should use !is_null rather than != NULL). Or, you could echo the node and user reference handlers and derive from MigrateSimpleFieldHandler, setting skip_empty to TRUE.
At mapping time, Migrate can't know whether NULL values make sense for the given field - that's up to the field handler. Interesting point about referencing the anonymous user, I think the skip_empty check should allow empty strings and zeros through, it should only filter out NULLs, I'll open an issue for that.
Comment #5
Damien Tournoud CreditAttribution: Damien Tournoud commentedOk, I committed the following.
Comment #6
thimothoeye CreditAttribution: thimothoeye commentedGuys, thanks for looking into this so quickly!
Comment #8
adrupaler CreditAttribution: adrupaler commentedI have the exact same problem. at first everything worked fine but after 3 test importing , the migrate module started to show this error even when my entity reference has value!
any body can help me why the entityreferencefieldhandler behave like this? also I modified the prepare() function to make sure the value isn't empty but nothing changed.