1

I am using migrate and migrate_d2d to migrate taxonomy terms from a d7 site to another d7 site.How can I specify in my migration that nothing be done if the term already exist in the target? So far, in "prepare" function, I am querying the target to find out if the term already exist, but I am not sure if this is the right way to go about it or where to go from here.

function prepare($entity, stdClass $row) {
  $term_name = $entity->name; 
  $query_checking_if_term_exist = db_select('taxonomy_term_data', 'ttd')
    ->condition('ttd.name', $term_name)
    /* TODO: fetch vid from taxonomy_vocabulary table instead of hardcoding it */
    ->condition('ttd.vid', '2')
    ->fields('ttd', array('name'));
  $term_exists = $query_checking_if_term_exist->execute()->fetchAll();
  if(isset($term_exists)) {
    //TODO
  }   
}
  • I'm not 100% sure on this, but have you tried unset($row); if the term exists? I believe they will delete that entry (skipping it). – Steven Jul 2 '14 at 20:35
0

If found the solution, if the term exists return false from prepareRow($row) and the term won't be updated or migrated again. like this:

public function prepareRow($row) {
  if (parent::prepareRow($row) === FALSE) {
    return FALSE;
  }
  $term_name = $row->name;
  $query_checking_if_term_exist = db_select('taxonomy_term_data', 'ttd')
    ->condition('ttd.name', $term_name)
    ->condition('ttd.vid', '2')
    ->fields('ttd', array('name'))
    ->execute()->fetchAll();
  if(!empty($query_checking_if_term_exist)) {
    return FALSE;
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.