簡體   English   中英

Drupal 7 | 遷移:如何將分類標簽按名稱關聯到節點?

[英]Drupal 7 | Migrate: how to assoc taxonomy tags to node by name?

我一直在嘗試設置我的遷移模塊一段時間,但未成功將分類學術語與節點相關聯。

首先要說的是,它不是 D2D遷移,來源是舊的symfony數據庫!

首先,我成功導出了分類法術語:

* - migrate.inc:

...
  'symfony_db_countries' => array(
    'class_name' => 'MigrateSymfonyCountriesMigration',
    'group_name' => 'symfony_db_loc',
  ),
  'symfony_db_cities' => array(
    'class_name' => 'MigrateSymfonyCitiesMigration',
    'group_name' => 'symfony_db_loc',
    'dependencies' => array('symfony_db_countries'),
  ),    
  'symfony_db_sights' => array(
    'class_name' => 'MigrateSymfonySightsMigration',
    'group_name' => 'symfony_db_loc',
  ),
...

Cities.inc:

class MigrateSymfonyCitiesMigration extends MigrateSymfonyCitiesMigrationBase {

  const TABLE_NAME = 'ya_loc_city';

  public function __construct($arguments = array()) {
    parent::__construct($arguments);

    $this->description = t('Migrate cities from Symfony DB.');

    $query = Database::getConnection('symfony', 'default')->select('ya_loc_city', 'c');
    $query->leftJoin('ya_loc_city_translation', 'ct', 'ct.id = c.id');
    $query->fields('c', array());
    $query->fields('ct', array());

    $this->source = new MigrateSourceSQL($query, array(), NULL, array('map_joinable' => FALSE));

    $this->destination = new MigrateDestinationTerm('cities');

    $this->map = new MigrateSQLMap($this->machineName,
        array(
          'id' => array(
            'type' => 'int',
            'not null' => TRUE,
            'description' => 'City ID',
          ),
        ),
        MigrateDestinationTerm::getKeySchema()
    );

    $this->addFieldMapping('name', 'name');
    $this->addFieldMapping('parent', 'country_id')->sourceMigration('symfony_db_countries');
  }
} 

然后,Im從另一個表創建節點,該表具有分類術語的entityReference字段(field_city)。

<?php
class MigrateSymfonySightsMigration extends Migration {
  public function prepareRow($row) {
    parent::prepareRow($row);
    $row->point = 'point (' . $row->longitude . ' ' . $row->latitude. ')';
  }

  public function __construct($arguments = array()) {
    //entry is the name of a group.
    parent::__construct($arguments);
    $this->description = 'Migration sights';

    /*************SOURCE DATA*************/
    ... 

    $this->source = new MigrateSourceSQL($query, array(), NULL,
      array('map_joinable' => FALSE));

    $this->destination = new MigrateDestinationNode('sight');

    /*************MAPPING*************/

    $this->addFieldMapping('field_city', 'name')->sourceMigration('symfony_db_cities');

    $this->map = new MigrateSQLMap($this->machineName,
       array(
         'id' => array('type' => 'int',
                            'unsigned' => TRUE,
                            'not null' => TRUE,
                      )
       ),
       MigrateDestinationNode::getKeySchema()
    );
  }
}

在symfony表中,有一列“名稱”,其中包含城市名稱,如“巴黎”。

如何將此列(“ 名稱 ”)值(“ 巴黎 ”)與分類術語相關聯? 以下不起作用:

$this->addFieldMapping('field_city', 'name')->sourceMigration('symfony_db_cities');

首先,請確保您的“ field_city”字段接受“ cities”字詞。

然后確保MigrateSymfonyCitiesMigration是MigrateSymfonySightsMigration的依賴項。

根據文檔 ,術語參考字段遷移默認情況下具有術語名稱的“ source_type”。 確保正確設置了“ ignore_case”。

如果其他所有方法均失敗,則將MigrateSymfonyCitiesMigration映射源ID1更改為“名稱”,而不是“ id”,然后重試(必須重新注冊遷移):

$this->map = new MigrateSQLMap($this->machineName,
    array(
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'City Name',
      ),
    ),
    MigrateDestinationTerm::getKeySchema()
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM