public function testAddArrayForeignKey() { $table = new Table('books'); $table->setDatabase($this->getDatabaseMock('bookstore')); $fk = $table->addForeignKey(array('name' => 'fk_author_id', 'phpName' => 'Author', 'refPhpName' => 'Books', 'onDelete' => 'CASCADE', 'foreignTable' => 'authors')); $this->assertInstanceOf('Propel\\Generator\\Model\\ForeignKey', $fk); $this->assertCount(1, $table->getForeignKeys()); $this->assertTrue($table->hasForeignKeys()); $this->assertContains('authors', $table->getForeignTableNames()); }
/** * Normalizes a table for the current platform. Very important for the TableComparator to not * generate useless diffs. * Useful for checking needed definitions/structures. E.g. Unique Indexes for ForeignKey columns, * which the most Platforms requires but which is not always explicitly defined in the table model. * * @param Table $table The table object which gets modified. */ public function normalizeTable(Table $table) { if ($table->hasForeignKeys()) { foreach ($table->getForeignKeys() as $fk) { if ($fk->getForeignTable() && !$fk->getForeignTable()->isUnique($fk->getForeignColumnObjects())) { $unique = new Unique(); $unique->setColumns($fk->getForeignColumnObjects()); $fk->getForeignTable()->addUnique($unique); } } } if (!$this->supportsIndexSize() && $table->getIndices()) { // when the plafform does not support index sizes we reset it foreach ($table->getIndices() as $index) { $index->resetColumnsSize(); } } foreach ($table->getColumns() as $column) { if ($column->getSize() && ($defaultSize = $this->getDefaultTypeSize($column->getType()))) { if (null === $column->getScale() && intval($column->getSize()) === $defaultSize) { $column->setSize(null); } } } }
/** * Unfortunately, SQLite does not support composite pks where one is AUTOINCREMENT, * so we have to flag both as NOT NULL and create a UNIQUE constraint. * * @param Table $table */ public function checkTable(Table $table) { if ($table->hasForeignKeys()) { foreach ($table->getForeignKeys() as $fk) { if (!$fk->getForeignTable()->isUnique($fk->getForeignColumnObjects())) { $unique = new Unique(); $unique->setColumns($fk->getForeignColumnObjects()); $fk->getForeignTable()->addUnique($unique); } } } if (count($pks = $table->getPrimaryKey()) > 1 && $table->hasAutoIncrementPrimaryKey()) { foreach ($pks as $pk) { //no pk can be NULL, as usual $pk->setNotNull(true); //in SQLite the column with the AUTOINCREMENT MUST be a primary key, too. if (!$pk->isAutoIncrement()) { //for all other sub keys we remove it, since we create a UNIQUE constraint over all primary keys. $pk->setPrimaryKey(false); } } //search if there is already a UNIQUE constraint over the primary keys $pkUniqueExist = false; foreach ($table->getUnices() as $unique) { $allPk = false; foreach ($unique->getColumns() as $columnName) { $allPk &= $table->getColumn($columnName)->isPrimaryKey(); } if ($allPk) { //there's already a unique constraint with the composite pk $pkUniqueExist = true; break; } } //there is none, let's create it if (!$pkUniqueExist) { $unique = new Unique(); foreach ($pks as $pk) { $unique->addColumn($pk); } $table->addUnique($unique); } } }
/** * Normalizes a table for the current platform. Very important for the TableComparator to not * generate useless diffs. * Useful for checking needed definitions/structures. E.g. Unique Indexes for ForeignKey columns, * which the most Platforms requires but which is not always explicitly defined in the table model. * * @param Table $table The table object which gets modified. */ public function normalizeTable(Table $table) { if ($table->hasForeignKeys()) { foreach ($table->getForeignKeys() as $fk) { if (!$fk->getForeignTable()->isUnique($fk->getForeignColumnObjects())) { $unique = new Unique(); $unique->setColumns($fk->getForeignColumnObjects()); $fk->getForeignTable()->addUnique($unique); } } } }