/** * @expectedException \RuntimeException * @expectedExceptionMessage "0" is not a valid foreign key option. */ public function testSetOptionThrowsExceptionIfOptionIsNotString() { $this->fk->setOptions(['update']); }
public function testProxyAdapterCanInvertAddForeignKey() { $table = new \Phinx\Db\Table('atable'); $refTable = new \Phinx\Db\Table('refTable'); $fk = new \Phinx\Db\Table\ForeignKey(); $fk->setReferencedTable($refTable)->setColumns(array('ref_table_id'))->setReferencedColumns(array('id')); $this->adapter->addForeignKey($table, $fk); $commands = $this->adapter->getInvertedCommands(); $this->assertEquals('dropForeignKey', $commands[0]['name']); $this->assertEquals('atable', $commands[0]['arguments'][0]); $this->assertContains('ref_table_id', $commands[0]['arguments'][1]); }
/** * Gets the SqlServer Foreign Key Definition for an ForeignKey object. * * @param ForeignKey $foreignKey * @return string */ protected function getForeignKeySqlDefinition(ForeignKey $foreignKey, $tableName) { $def = ' CONSTRAINT "'; $def .= $tableName . '_' . implode('_', $foreignKey->getColumns()); $def .= '" FOREIGN KEY ("' . implode('", "', $foreignKey->getColumns()) . '")'; $def .= " REFERENCES {$foreignKey->getReferencedTable()->getName()} (\"" . implode('", "', $foreignKey->getReferencedColumns()) . '")'; if ($foreignKey->getOnDelete()) { $def .= " ON DELETE {$foreignKey->getOnDelete()}"; } if ($foreignKey->getOnUpdate()) { $def .= " ON UPDATE {$foreignKey->getOnUpdate()}"; } return $def; }
/** * Gets the MySQL Foreign Key Definition for an ForeignKey object. * * @param ForeignKey $foreignKey * @return string */ protected function getForeignKeySqlDefinition(ForeignKey $foreignKey) { $def = ''; if ($foreignKey->getConstraint()) { $def .= ' CONSTRAINT ' . $this->quoteColumnName($foreignKey->getConstraint()); } $columnNames = array(); foreach ($foreignKey->getColumns() as $column) { $columnNames[] = $this->quoteColumnName($column); } $def .= ' FOREIGN KEY (' . implode(',', $columnNames) . ')'; $refColumnNames = array(); foreach ($foreignKey->getReferencedColumns() as $column) { $refColumnNames[] = $this->quoteColumnName($column); } $def .= ' REFERENCES ' . $this->quoteTableName($foreignKey->getReferencedTable()->getName()) . ' (' . implode(',', $refColumnNames) . ')'; if ($foreignKey->getOnDelete()) { $def .= ' ON DELETE ' . $foreignKey->getOnDelete(); } if ($foreignKey->getOnUpdate()) { $def .= ' ON UPDATE ' . $foreignKey->getOnUpdate(); } return $def; }
/** * Add a foreign key to a database table. * * In $options you can specify on_delete|on_delete = cascade|no_action .., * on_update, constraint = constraint name. * * @param string|array $columns Columns * @param string|Table $referencedTable Referenced Table * @param string|array $referencedColumns Referenced Columns * @param array $options Options * @return Table */ public function addForeignKey($columns, $referencedTable, $referencedColumns = array('id'), $options = array()) { if (is_string($referencedColumns)) { $referencedColumns = array($referencedColumns); // str to array } $fk = new ForeignKey(); if ($referencedTable instanceof Table) { $fk->setReferencedTable($referencedTable); } else { $fk->setReferencedTable(new Table($referencedTable, array(), $this->adapter)); } $fk->setColumns($columns)->setReferencedColumns($referencedColumns)->setOptions($options); $this->foreignKeys[] = $fk; return $this; }
/** * @expectedException \InvalidArgumentException */ public function testUnknownActionsNotAlowedThroughOptions() { $this->fk->setOptions(array('update' => 'no yu a dumb')); }
/** * Utility method that maps an array of index options to this objects methods. * * @param array $options Options * * @return $this */ public function setOptions(array $options) { $this->foreign->setOptions($options); return $this; }