I have implemented
MyMigration(XMLMigration)::prepare() which is setting a couple of things in the node object depending on the data that is being imported, however
plugins/node.inc line 195 calls plugins/destinations/entity.inc, entity.inc executes $migration->prepare() but the results of that are never visible/available to that initial call on plugins/node.inc line 195
/**
* Give handlers a shot at modifying the object before saving it.
*
* @param $entity
* Entity object to build. Prefilled with any fields mapped in the Migration.
* @param $source_row
* Raw source data object - passed through to prepare handlers.
*/
public function prepare($entity, stdClass $source_row) {
...
// Then call any prepare handler for this specific Migration.
if (method_exists($migration, 'prepare')) {
$migration->prepare($entity, $source_row);
}
...
which means anything i modify in my prepare() statement is ignored, NOTE: I am running this as an 'update'
ideas?
Comments
Comment #1
dgtlmoon commentedForcing a reference seems to fix my issues, might be a clue in here somewhere..
(partial patch here just to show the issue)
as well as using a reference in my prepare() of my implementation
Comment #2
mikeryanAdding an & should not make any difference - objects are effectively passed by reference in PHP (you would only need an & if you were trying to change $node itself, e.g. $node = new stdClass - not if you're changing the contents of the node e.g. $node->title).
How are you confirming that your changes are made in $migration->prepare() but are not present after the call to $this->prepare() in MigrateDestinationNode::import()?
Comment #3
dgtlmoon commentedBecause if i set a breakpoint in my debugger when the call to ->prepare() returns the variables arent being set the node, and as a result, taxonomy terms arent being saved that i'm specifically setting in my prepare()
also the 'vague patch' supplied makes the import work, without it, it doesnt work
Comment #4
mikeryanWhat does your migration prepare() handler look like? Are you by any chance assigning directly to $node itself, instead of just modifying fields within $node? That's the only scenario I can imagine where making the parameter a reference would make it work for you.
To test, I added this to BeerNodeMigration in migration_example's beer.inc:
When I do drush_print_r($node) after the call to prepare() in node.inc, the foobar field is there.
Comment #5
dgtlmoon commentedWhat if i wanted to do some string fudging like
should this be done in `prepare` ? doesnt seem to work for me
Comment #6
mikeryanThat should work - have you tried a drush_print_r($node) after the call to prepare() in node.inc to confirm it's not changing?
Comment #7
mikeryanNo further information provided.
Comment #8
dgtlmoon commentedOK great, figured it out, I was confusing prepare() with prepareRow(), sorry about that!
Comment #9
cameron tod commentedIn migrating some quiz nodes from Drupal 6 to Drupal 7, I experienced this very thing. Quiz needs some fields set on the node before node_save(), or it will freak out about some columns being null.
After lots of debugging, and reading this issue, it seems that changing the $entity object is not permitted. I can change its values, but not its members.
So, adding a & to $entity in MigrateDestinationEntity::prepare() and in my local Migration class seems to fix it right up. I can now add fields to the node object before it is saved.
Here's how my migration is set up:
Drupal6NodeMigration comes from drupal_to_drupal_data_migration, and implements MigrateDestinationNode.
I've attached a patch, hopefully it is useful to someone.
Comment #11
mikeryanThe problem is that you are changing the $entity variable itself, not its contents:
This is the proper way to update the object:
Comment #12
cameron tod commentedWicked, thanks for that tip.
Comment #13
mikeryan