The method will properly quote the table and column names.
public addForeignKey ( string $name, string $table, string | array $columns, string $refTable, string | array $refColumns, string $delete = null, string $update = null ) | ||
$name | string | the name of the foreign key constraint. |
$table | string | the table that the foreign key constraint will be added to. |
$columns | string | array | the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas or use an array. |
$refTable | string | the table that the foreign key references to. |
$refColumns | string | array | the name of the column that the foreign key references to. If there are multiple columns, separate them with commas or use an array. |
$delete | string | the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
$update | string | the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL |
public function install() { parent::install(); $migration = new Migration(); $migration->createTable($this->getTable(), ['id' => Schema::TYPE_PK, 'object_id' => Schema::TYPE_INTEGER, 'term_id' => Schema::TYPE_BIGINT, 'value' => Schema::TYPE_STRING]); if ($migration->db->driverName === 'mysql') { $migration->addForeignKey('fk_' . $this->getTable() . '_' . $this->getRefTableName(), $this->getTable(), 'object_id', $this->getRefTableName(), 'id', 'CASCADE'); $migration->addForeignKey('fk_' . $this->getTable() . '_' . TaxonomyTerms::tableName(), $this->getTable(), 'term_id', TaxonomyTerms::tableName(), 'id', 'CASCADE'); } }
/** * Update ip-geo-base data * * @throws \yii\base\Exception */ public function actionUpdate() { $migrate = new Migration(); $migrate->dropForeignKey('fk-geobase_contact-geobase_city_id', 'geobase_contact'); $ipGeoBase = new IpGeoBase(); $ipGeoBase->updateDB(); $migrate->addForeignKey('fk-geobase_contact-geobase_city_id', 'geobase_contact', 'geobase_city_id', 'geobase_city', 'id', 'CASCADE', 'CASCADE'); }
/** * Relate creations * @return bool */ public function runRelations() { $result = true; foreach ($this->relations as $_nextRelation) { try { if ($this->hideMigrationOutput) { ob_start(); } $result = $this->migrationClass->addForeignKey($_nextRelation['fk_name'], $_nextRelation['table_name'], $_nextRelation['field'], $_nextRelation['related_table'], $_nextRelation['related_field'], 'RESTRICT', 'CASCADE') && $result; if ($this->hideMigrationOutput) { ob_clean(); ob_flush(); } } catch (\yii\db\Exception $expt) { } } return $result; }
/** * @inheritdoc * Note: tables will be auto pefixied if [[$autoWrapTableNames]] is true. */ public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) { $table = $this->autoWrappedTableName($table); $refTable = $this->autoWrappedTableName($refTable); return parent::addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update); }
/** * @inheritdoc * @param string|null $name */ public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) { if (is_null($name)) { $name = implode('-', array_merge((array) $table, (array) $columns)); } if (is_null($delete)) { $delete = static::RESTRICT; } if (is_null($update)) { $update = static::NO_ACTION; } parent::addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update); }
public function apply() { return $this->migrate->addForeignKey($this->getName(), $this->getSourceTable(), $this->getSourceColumn(), $this->getRefTable(), $this->getRefColumn(), $this->getOnDelete(), $this->getOnUpdate()); }
/** * Builds a SQL statement for adding a foreign key constraint to an existing table (without index creation). * The method will properly quote the table and column names. * @param string $name the name of the foreign key constraint. * @param string $table the table that the foreign key constraint will be added to. * @param string|array $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas or use an array. * @param string $refTable the table that the foreign key references to. * @param string|array $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas or use an array. * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL * * @see addForeignKey */ public function addForeignKeyWithoutIndex($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) { parent::addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update); }
/** * Builds a SQL statement for adding a foreign key constraint to an existing table. * The method will properly quote the table and column names. * @param string $table the table that the foreign key constraint will be added to. * @param string|array $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas or use an array. * @param string $refTable the table that the foreign key references to. * @param string|array $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas or use an array. * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL * @param string $name the name of the foreign key constraint. */ public function addForeignKey($table, $columns, $refTable, $refColumns, $delete = 'CASCADE', $update = 'CASCADE', $name = null) { $name = $name ?: $this->getNameForeignKey($table, $columns); parent::addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update); }
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) { if (is_null($name)) { $name = self::formFkName($table, $columns, $refTable, $refColumns); } return parent::addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete, $update); }