Closed (works as designed)
Project:
Migrate Extras
Version:
7.x-2.3
Component:
Profile2
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
24 May 2012 at 16:07 UTC
Updated:
28 Feb 2013 at 13:15 UTC
I am trying to migrate profile from D6 to D7 and I am trying to migrate just the first name values for making it work and getting fatal error. I am not sure what I am doing wrong.
Fatal error: Cannot access empty property in field/field.attach.inc on line 197.
/*
*migration class to test import of various date fields.
*/
class ProfileMigration extends Migration {
public $destinationDBName = 'abc';
public function __construct() {
parent::__construct();
$this->description = t('Example migration into profile2 entities');
$this->map = new MigrateSQLMap($this->machineName,
array(
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
)
),
MigrateDestinationProfile2::getKeySchema()
);
// We are getting data from Profile table from the shared database so create connection and set up a query for this data.
$query = db_select('profile_values', 'p1')
->fields('p1', array('fid', 'uid', 'value'))
->condition('p1.fid', array('50', '1'), 'IN')
->orderBy('p1.fid', 'DESC');
$this->source = new MigrateSourceSQL($query);
$this->destination = new MigrateDestinationProfile2($bundle);
// Assign mappings TO destination fields FROM source fields. To discover
$this->addFieldMapping('field_first_name_value', 'name');
}
/**
* We get the opportunity to force changes in post mapping here.
*/
public function prepare($entity, $row) {
var_dump($entity);
var_dump($row);
db_set_active($destinationDBName);
$query = db_select('field_data_field_first_name', 'p2')
->fields('p2', array('entity_id'))
->condition('p2.entity_id', $entity->entity_id);
$results = $query->execute();
db_set_active('default');
}
}
Comments
Comment #0.0
kgoel commentedUpdated issue summary.
Comment #0.1
kgoel commentedUpdated issue summary.
Comment #0.2
kgoel commentedUpdated issue summary.
Comment #1
mikeryanThis needs a lot of work. First off, there's no reason to use db_set_active() - see http://drupal.org/node/1014558 for information on how to access external databases. At any rate, you seem to be trying to query the destination database, which is the default database anyway. And you're querying for a first name value that doesn't yet exist, using an entity_id that doesn't yet exist (prepare is called before the node is saved). And since it's a node, that would be nid, not entity_id, in the $entity object (if it had been set).
Now, to the likely immediate cause of the specific warning you got:
In your query, there is no 'name' being returned. This query isn't going to work right anyway, because it's going to go through one field at a time. Take a look at http://drupal.org/node/1593778#comment-6026880 for an example of how to get your Drupal 6 profile fields properly populated.
Comment #2
kgoel commentedMike - Again thanks for the quick reply. I am using Profile2, migrate extras and migrate modules.
I have created profile type(for eg. User Profile) into D7 site and added fields like first name, last name , city and so on to the profile type so the field_first_name_value field exits in field_data_field_first_name table but doesn't have any data into that.
In the above code I am trying migration just for the first name to make my code work and than later worry about rest of the fields.
Comment #3
kgoel commentedExample in the Migrate Extras module works for the CSV file and its simple. Is there any other example I can take a look for this specific case?
Comment #4
kgoel commentedI have created database connection in the settings.php for the source database and made some changes to the code as suggested. Not sure why its still not working.
<?php
/*
*migration class to test import of various date fields.
*/
class ProfileMigration extends Migration {
public function __construct() {
parent::__construct();
$this->description = t('Example migration into profile2 entities');
$this->map = new MigrateSQLMap($this->machineName,
array(
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
)
),
MigrateDestinationProfile2::getKeySchema()
);
$this->highwaterField = array(
'name' => 'uid',
'alias' => 'pv',
'type' => 'int',
);
// We are getting data from Profile table from the shared database so create connection and set up a query for this data.
$query = Database::getConnection('default', 'abc')
-> db_select('profile_values', 'pv')
->left_join('profile_fields', 'pf', 'pv.fid = pf.fid')
// ->fields('pf', array('fid', 'title', 'name', 'explanation', 'category', 'page', 'type', 'weight', 'required', 'register', 'visibility', 'autocomplete', 'options'))
->fields('pv', array('fid', 'uid', 'value'))
->fields('pf', array('name'))
->condition('pv.fid', array('50', '1'), 'IN')
->orderBy('pv.uid', 'DESC');
$this->source = new MigrateSourceSQL($query);
$this->destination = new MigrateDestinationProfile2($bundle);
// Assign mappings TO destination fields FROM source fields. To discover
$this->addFieldMapping('field_first_name', 'profile_first_name');
$this->addFieldMapping('field_last_name', 'profile_last_name');
$this->addFieldmapping('field_intro', 'profile_intro');
$this->addFieldMapping('field_tag', 'profile_tag');
$this->addFieldMapping('field_city', 'profile_city');
$this->addFieldMapping('field_state', 'profile_state');
$this->addFieldMapping('field_zip', 'profile_zip');
$this->addFieldMapping('field_country', 'profile_country');
$this->addFieldMapping('is_new', '')->defaultValue(TRUE);
}
public function prepareRow($row) {
$profile_fields = Database::getConnection('default', 'abc')
->select('profile_values', 'pv')
->fields('pv', array('fid', 'value')
->condition('uid', $row->uid)
->execute()
->fetchAllKeyed();
$this->profile_first_name = $profile_fields[1];
$this->profile_last_name = $profile_fields[2];
$this->profile_intro = $profile_fields[3];
$this->profile_tag = $profile_fields[4];
$this->profile_city = $profile_fields[5];
$this->profile_state = $profile_fields[6];
$this->profile_zip = $profile_fields[7];
$this->profile_country = $profile_fields[8];
}
Comment #5
mikeryanThe query needs to be returning one row per user. In this case, since all you need per user is the uid, you can just do
(note: for your PHP code to be properly formatted here, you need the closing ?>).
I also notice you're passing $bundle to MigrateDestinationProfile2, but not setting it first.
Comment #6
mikeryanComment #7
peteruithoven commentedI just discovered profiles are almost automatically migrated when you use the Drupal-to-Drupal data migration module.
http://drupal.org/project/migrate_d2d
You just need to add some field mappings, I needed for example:
You can get all these names from the migrate interface where you can see what's mapped and what's not.
(Great stuff Mike!)
Comment #8
visualfox commentedHello kgoel,
In the prepareRow function you should modify the $row variable not the $this (which is the migrate object itself), based on your example:
need to be
Comment #8.0
visualfox commentedFix php tagging