Hi,

I was wondering how to integrate entityreferences with Migrate.

To be specific, I'm trying to migrate users from a SQL Server database. They contain a column "storeLocation" where they're associated with a store.

On my Drupal site, I have created an EntityReference field in their profile called "Favorite Store", which maps them to a store location. The machine name of the field is "field_ad_town" which may contain an array. In one case, I found this in devel:

field_ad_town[] (Array, 1 element)
und (Array, 3 elements)
0 (Array, 1 element)
target_id(String, 2 characters): 71
1 (Array, 1 element)
target_id(String, 2 characters): 72
2 (Array, 1 element)
target_id(String, 2 characters): 74

I should mention that users from my Java website will NOT have more than one "storeLocation." Based on the data that I have, I believe that users may be mapped only to one location. It is in special cases that more than 1 location are assigned. Thus, this simplifies a bit.

Looking at the Migrate module, I believe that the best way to do this is:

// When mapping fields...

$this->addFieldMapping('field_ad_town', 'locationName')
    ->defaultValue('')
    ->description(t('The location name of their favorite store. Used for advertisements.'));

Does this seem like the correct way of mapping out these fields?

Comments

derhasi’s picture

You first could migrate the entities to reference as standalone and then map the new id to the entityreference using ->sourceMigration.

EDIT: Here's the documentation on multiple rows: http://drupal.org/node/1012810

jeisses’s picture

I have the same problem, I can't figure out how to migrate to an entity reference.
How can I migrate the entities to reference as standalone (like derhase suggested)? Is there a fieldmapping for this? I'm a bit lost, it would be great if someone could point me in the right direction.

Damien Tournoud’s picture

If you are migrating the stores separately, you want something like:

$this->addFieldMapping('field_ad_town', 'locationName')
    ->description(t('The location name of their favorite store. Used for advertisements.'))
    ->sourceMigration('TheNameOfTheStoreMigration');

That should work out of the box.

jeisses’s picture

[EDIT]
Thanks for the help!
I had some difficulty grasping the concept of entity reference; my problem had to do with illegal mappings in the legacy db.
The above solution works great.

Damien Tournoud’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jhodgdon’s picture

Version: 7.x-1.0-rc1 » 7.x-1.0
Status: Closed (fixed) » Active

I'm working on a migration from a D6 nodereference CCK field to a D7 entity reference field... I'm using Migrate 7.x-2.5 and Entityreference 7.x-1.0, and I'm trying to import a multi-valued node reference. It's not working for me at all.

I thought it might be due to:
#1891480: Migration syntaz depends on field widget type
but changing the widget on my D7 entity reference field is not working either (I tried checkboxes/radios, select list, and Views).

In my migration class, as suggested above, I have done this... $source_name is the field name in my old site, which is the same as $name in this case (the field in my new site); $migrate is the name of the migration of the nodes that are being referenced, and I've verified using the Migrate UI that the field mappings are correct:

 $this->addFieldMapping($name, $source_name)->sourceMigration(array($migrate));

I've also verified that at the end of my prepareRow() method, the field (with the correct name) is filled with an array of the source node IDs, and I looked in the migrate database and the mappings of node IDs for the migration I told it to use are fine.

But nothing is being saved in the entity reference field. I looked in the database and there's nothing there, although I have 30 node IDs in my prepared row. Is there some other manipulation I need to do, or some other format I need to use for the data? When migrating term references and other types of fields, it seems like I just need to put the values in there as an array, but it's just not working for the node references.

Help?

jhodgdon’s picture

Oh, and I'm also not getting any error messages from the migration.

jhodgdon’s picture

Status: Active » Closed (fixed)

Ah. It turned out that this was due to a bug in the entitreference.migrate.inc file that exists in 7.x-1.0 but has been fixed in the git repo. Sigh. If anyone else encounters this, you need to find this line in entityreference.migrate.inc:

    'field_handlers' => array('MigrateEntityReferenceFieldHandler'),

and change it to:

    'field handlers' => array('MigrateEntityReferenceFieldHandler'),

Note the lack of underscore in "field handlers".

A new release could be helpful, if anyone is reading this issue. :)

jhodgdon’s picture

#1845986: Can't migrate to entity reference field was the issue where that fix was made apparently.

tomasbarrios’s picture

Thanks #10!!!