On some of my migrations, the source table is missing values for some of the fields.
When migrating into Drupal, it still imports that missing value.
So some fields have a label, but no value.
If I even just edit and save the node (with no other changes), the blank value is removed [and the label no longer shows].

Is there any way to avoid these 'Phantom Values'?

Comments

Andrey Zakharov’s picture

implement method YourMigration::prepareRow

public function prepareRow($row) {
      if (empty($row->default_zero_field))
         unset($row->default_zero_field);
}
alanburke’s picture

Status: Active » Fixed

Thanks Andrey!
I implemented a cleanup function in my base class

      public function __prepareRow($current_row) {
        foreach($current_row as $field=>$value){
          if (empty($value) && $value !="0"){
             unset($current_row->$field);
          }
        }
      }

and I call that within prepare_Row for each migration.

Status: Fixed » Closed (fixed)

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

drclaw’s picture

Version: 7.x-2.x-dev » 7.x-2.5
Status: Closed (fixed) » Active

I don't think this solution works anymore... I'm using migrate-7.x-2.5 and I'm still getting fields populated with empty values. (e.g. $node->field_name['und'][0]['value'] == 0). I took a look at Migration::applyMappings() and it looks like the destination object has a field value set regardless of whether or not it's corresponding source field is set. I could be wrong though... has anyone else got this method above to work?

chrisakeley’s picture

This solution did not work for me, either. Even when using unset() in prepareRow, migrate created blank rows in the field_data_XX table for text fields. It looks like using removeFieldMapping DOES work, however. For example:
if (!strlen($row->my_text_field) $this->removeFieldMapping('my_text_field');

drclaw’s picture

Ah, I see. Thanks for mentioning that!

mikeryan’s picture

Version: 7.x-2.5 » 7.x-2.x-dev
Status: Active » Closed (fixed)

Please don't reopen long-closed issues - look for a relevant open issue (consider if #1792894: Set configured defaults for empty fields applies) or open a new issue.

Thanks.

ronino’s picture

None of the above solutions works for me with version 2.5. What works for me instead in prepareRow() is to assign an empty array to those fields:

public function prepareRow($row) {
  if (empty($row->my_field)) {
    $row->my_field = array();
  }
}
pmkanse’s picture

Thanks @Ronino, it works!!!!