/** * Gather columns and fk constraints that are required for one part of relationship. * * @param array $joinColumns * @param \Doctrine\DBAL\Schema\Table $theJoinTable * @param ClassMetadata $class * @param array $mapping * @param array $primaryKeyColumns * @param array $uniqueConstraints */ private function _gatherRelationJoinColumns($joinColumns, $theJoinTable, $class, $mapping, &$primaryKeyColumns, &$uniqueConstraints) { $localColumns = array(); $foreignColumns = array(); $fkOptions = array(); $foreignTableName = $this->quoteStrategy->getTableName($class, $this->platform); foreach ($joinColumns as $joinColumn) { list($definingClass, $referencedFieldName) = $this->getDefiningClass($class, $joinColumn['referencedColumnName']); if (!$definingClass) { throw new \Doctrine\ORM\ORMException("Column name `" . $joinColumn['referencedColumnName'] . "` referenced for relation from " . $mapping['sourceEntity'] . " towards " . $mapping['targetEntity'] . " does not exist."); } $quotedColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); $quotedRefColumnName = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $class, $this->platform); $primaryKeyColumns[] = $quotedColumnName; $localColumns[] = $quotedColumnName; $foreignColumns[] = $quotedRefColumnName; if (!$theJoinTable->hasColumn($quotedColumnName)) { // Only add the column to the table if it does not exist already. // It might exist already if the foreign key is mapped into a regular // property as well. $fieldMapping = $definingClass->getFieldMapping($referencedFieldName); $columnDef = null; if (isset($joinColumn['columnDefinition'])) { $columnDef = $joinColumn['columnDefinition']; } else { if (isset($fieldMapping['columnDefinition'])) { $columnDef = $fieldMapping['columnDefinition']; } } $columnOptions = array('notnull' => false, 'columnDefinition' => $columnDef); if (isset($joinColumn['nullable'])) { $columnOptions['notnull'] = !$joinColumn['nullable']; } if (isset($fieldMapping['options'])) { $columnOptions['options'] = $fieldMapping['options']; } if ($fieldMapping['type'] == "string" && isset($fieldMapping['length'])) { $columnOptions['length'] = $fieldMapping['length']; } else { if ($fieldMapping['type'] == "decimal") { $columnOptions['scale'] = $fieldMapping['scale']; $columnOptions['precision'] = $fieldMapping['precision']; } } $theJoinTable->addColumn($quotedColumnName, $fieldMapping['type'], $columnOptions); } if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) { $uniqueConstraints[] = array('columns' => array($quotedColumnName)); } if (isset($joinColumn['onDelete'])) { $fkOptions['onDelete'] = $joinColumn['onDelete']; } } $theJoinTable->addUnnamedForeignKeyConstraint($foreignTableName, $localColumns, $foreignColumns, $fkOptions); }
public function testDropColumn() { $type = Type::getType('integer'); $columns = array(); $columns[] = new Column("foo", $type); $columns[] = new Column("bar", $type); $table = new Table("foo", $columns, array(), array()); $this->assertTrue($table->hasColumn("foo")); $this->assertTrue($table->hasColumn("bar")); $table->dropColumn("foo")->dropColumn("bar"); $this->assertFalse($table->hasColumn("foo")); $this->assertFalse($table->hasColumn("bar")); }
/** * Adds the missing columns to the table * * @param \Doctrine\DBAL\Schema\Table $table Table object * @return \Doctrine\DBAL\Schema\Table Updated table object */ protected function addColumns(\Doctrine\DBAL\Schema\Table $table) { $columns = array('company' => array('string', array('length' => 100)), 'vatid' => array('string', array('length' => 32)), 'salutation' => array('string', array('length' => 8)), 'title' => array('string', array('length' => 64)), 'firstname' => array('string', array('length' => 64)), 'lastname' => array('string', array('length' => 64)), 'address1' => array('string', array('length' => 255)), 'address2' => array('string', array('length' => 255)), 'address3' => array('string', array('length' => 255)), 'postal' => array('string', array('length' => 16)), 'city' => array('string', array('length' => 255)), 'state' => array('string', array('length' => 255)), 'langid' => array('string', array('length' => 5, 'notnull' => false)), 'countryid' => array('string', array('length' => 2, 'notnull' => false, 'fixed' => true)), 'telephone' => array('string', array('length' => 32)), 'telefax' => array('string', array('length' => 32)), 'website' => array('string', array('length' => 255)), 'birthday' => array('date', array('notnull' => false)), 'vdate' => array('date', array('notnull' => false)), 'status' => array('smallint', array()), 'mtime' => array('datetime', array()), 'ctime' => array('datetime', array()), 'editor' => array('string', array('length' => 255))); foreach ($columns as $name => $def) { if ($table->hasColumn($name) === false) { $table->addColumn($name, $def[0], $def[1]); } } return $table; }
/** * Adds the missing columns to the table * * @param \Doctrine\DBAL\Schema\Table $table Table object * @return \Doctrine\DBAL\Schema\Table Updated table object */ protected function addColumns(\Doctrine\DBAL\Schema\Table $table) { $columns = array('cdate' => array('string', array('fixed' => 10)), 'cmonth' => array('string', array('fixed' => 7)), 'cweek' => array('string', array('fixed' => 7)), 'chour' => array('string', array('fixed' => 2))); foreach ($columns as $name => $def) { if ($table->hasColumn($name) === false) { $table->addColumn($name, $def[0], $def[1]); } } return $table; }
/** * Determine if a column already exists. * * @param string $column * @param bool $throwException * * @return bool * * @throws SchemaException */ public function checkColumnExists($column, $throwException = false) { //check to ensure column doesn't exist if ($this->toTable->hasColumn($column)) { if ($throwException) { throw new SchemaException("The column {$column} already exists in {$this->tableName}"); } return true; } return false; }
/** * Add a foreign key constraint with a given name * * @param string $name * @param Table $foreignTable * @param array $localColumns * @param array $foreignColumns * @param array $options * @return Table */ public function addNamedForeignKeyConstraint($name, $foreignTable, array $localColumnNames, array $foreignColumnNames, array $options = array()) { if ($foreignTable instanceof Table) { $foreignTableName = $foreignTable->getName(); foreach ($foreignColumnNames as $columnName) { if (!$foreignTable->hasColumn($columnName)) { throw SchemaException::columnDoesNotExist($columnName, $foreignTable->getName()); } } } else { $foreignTableName = $foreignTable; } foreach ($localColumnNames as $columnName) { if (!$this->hasColumn($columnName)) { throw SchemaException::columnDoesNotExist($columnName, $this->_name); } } $constraint = new ForeignKeyConstraint($localColumnNames, $foreignTableName, $foreignColumnNames, $name, $options); $this->_addForeignKeyConstraint($constraint); return $this; }
/** * Gather columns and fk constraints that are required for one part of relationship. * * @param array $joinColumns * @param \Doctrine\DBAL\Schema\Table $theJoinTable * @param ClassMetadata $class * @param \Doctrine\ORM\Mapping\AssociationMapping $mapping * @param array $primaryKeyColumns * @param array $uniqueConstraints */ private function _gatherRelationJoinColumns($joinColumns, $theJoinTable, $class, $mapping, &$primaryKeyColumns, &$uniqueConstraints) { $localColumns = array(); $foreignColumns = array(); $fkOptions = array(); foreach ($joinColumns as $joinColumn) { // Note that this thing might be quoted, i.e. `foo`, [foo], ... $columnName = $mapping->getQuotedJoinColumnName($joinColumn['name'], $this->_platform); $referencedFieldName = $class->getFieldName($joinColumn['referencedColumnName']); if (!$class->hasField($referencedFieldName)) { throw new \Doctrine\Common\DoctrineException("Column name `" . $joinColumn['referencedColumnName'] . "` referenced for relation from " . "{$mapping->sourceEntityName} towards {$mapping->targetEntityName} does not exist."); } $primaryKeyColumns[] = $columnName; $localColumns[] = $columnName; $foreignColumns[] = $joinColumn['referencedColumnName']; if (!$theJoinTable->hasColumn($joinColumn['name'])) { // Only add the column to the table if it does not exist already. // It might exist already if the foreign key is mapped into a regular // property as well. $fieldMapping = $class->getFieldMapping($referencedFieldName); $columnDef = null; if (isset($joinColumn['columnDefinition'])) { $columnDef = $joinColumn['columnDefinition']; } else { if (isset($fieldMapping['columnDefinition'])) { $columnDef = $fieldMapping['columnDefinition']; } } $columnOptions = array('notnull' => false, 'columnDefinition' => $columnDef); if (isset($joinColumn['nullable'])) { $columnOptions['notnull'] = !$joinColumn['nullable']; } $theJoinTable->createColumn($columnName, $class->getTypeOfColumn($joinColumn['referencedColumnName']), $columnOptions); } if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) { $uniqueConstraints[] = array('columns' => array($columnName)); } if (isset($joinColumn['onUpdate'])) { $fkOptions['onUpdate'] = $joinColumn['onUpdate']; } if (isset($joinColumn['onDelete'])) { $fkOptions['onDelete'] = $joinColumn['onDelete']; } } $theJoinTable->addUnnamedForeignKeyConstraint($class->getQuotedTableName($this->_platform), $localColumns, $foreignColumns, $fkOptions); }
/** * Returns the difference between the tables $table1 and $table2. * * If there are no differences this method returns the boolean false. * * @param \Doctrine\DBAL\Schema\Table $table1 * @param \Doctrine\DBAL\Schema\Table $table2 * * @return boolean|\Doctrine\DBAL\Schema\TableDiff */ public function diffTable(Table $table1, Table $table2) { $changes = 0; $tableDifferences = new TableDiff($table1->getName()); $tableDifferences->fromTable = $table1; $table1Columns = $table1->getColumns(); $table2Columns = $table2->getColumns(); /* See if all the fields in table 1 exist in table 2 */ foreach ($table2Columns as $columnName => $column) { if (!$table1->hasColumn($columnName)) { $tableDifferences->addedColumns[$columnName] = $column; $changes++; } } /* See if there are any removed fields in table 2 */ foreach ($table1Columns as $columnName => $column) { // See if column is removed in table 2. if (!$table2->hasColumn($columnName)) { $tableDifferences->removedColumns[$columnName] = $column; $changes++; continue; } // See if column has changed properties in table 2. $changedProperties = $this->diffColumn($column, $table2->getColumn($columnName)); if (!empty($changedProperties)) { $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties); $columnDiff->fromColumn = $column; $tableDifferences->changedColumns[$column->getName()] = $columnDiff; $changes++; } } // #BUG-2317 Avoid column renaming when both enable and disable different modules // $this->detectColumnRenamings($tableDifferences); $table1Indexes = $table1->getIndexes(); $table2Indexes = $table2->getIndexes(); foreach ($table2Indexes as $index2Name => $index2Definition) { foreach ($table1Indexes as $index1Name => $index1Definition) { if ($this->diffIndex($index1Definition, $index2Definition) === false) { /*if ( ! $index1Definition->isPrimary() && $index1Name != $index2Name) { $tableDifferences->renamedIndexes[$index1Name] = $index2Definition; $changes++; }*/ unset($table1Indexes[$index1Name]); unset($table2Indexes[$index2Name]); } else { if ($index1Name == $index2Name) { $tableDifferences->changedIndexes[$index2Name] = $table2Indexes[$index2Name]; unset($table1Indexes[$index1Name]); unset($table2Indexes[$index2Name]); $changes++; } } } } foreach ($table1Indexes as $index1Name => $index1Definition) { $tableDifferences->removedIndexes[$index1Name] = $index1Definition; $changes++; } foreach ($table2Indexes as $index2Name => $index2Definition) { $tableDifferences->addedIndexes[$index2Name] = $index2Definition; $changes++; } $fromFkeys = $table1->getForeignKeys(); $toFkeys = $table2->getForeignKeys(); foreach ($fromFkeys as $key1 => $constraint1) { foreach ($toFkeys as $key2 => $constraint2) { if ($this->diffForeignKey($constraint1, $constraint2) === false) { unset($fromFkeys[$key1]); unset($toFkeys[$key2]); } else { if (strtolower($constraint1->getName()) == strtolower($constraint2->getName())) { $tableDifferences->changedForeignKeys[] = $constraint2; $changes++; unset($fromFkeys[$key1]); unset($toFkeys[$key2]); } } } } foreach ($fromFkeys as $constraint1) { $tableDifferences->removedForeignKeys[] = $constraint1; $changes++; } foreach ($toFkeys as $constraint2) { $tableDifferences->addedForeignKeys[] = $constraint2; $changes++; } return $changes ? $tableDifferences : false; }
/** * Gathers columns and fk constraints that are required for one part of relationship. * * @param array $joinColumns * @param Table $theJoinTable * @param ClassMetadata $class * @param array $mapping * @param array $primaryKeyColumns * @param array $addedFks * @param array $blacklistedFks * * @return void * * @throws \Doctrine\ORM\ORMException */ private function gatherRelationJoinColumns($joinColumns, $theJoinTable, $class, $mapping, &$primaryKeyColumns, &$addedFks, &$blacklistedFks) { $localColumns = array(); $foreignColumns = array(); $fkOptions = array(); $foreignTableName = $this->quoteStrategy->getTableName($class, $this->platform); $uniqueConstraints = array(); foreach ($joinColumns as $joinColumn) { list($definingClass, $referencedFieldName) = $this->getDefiningClass($class, $joinColumn['referencedColumnName']); if (!$definingClass) { throw new \Doctrine\ORM\ORMException("Column name `" . $joinColumn['referencedColumnName'] . "` referenced for relation from " . $mapping['sourceEntity'] . " towards " . $mapping['targetEntity'] . " does not exist."); } $quotedColumnName = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform); $quotedRefColumnName = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $class, $this->platform); $primaryKeyColumns[] = $quotedColumnName; $localColumns[] = $quotedColumnName; $foreignColumns[] = $quotedRefColumnName; if (!$theJoinTable->hasColumn($quotedColumnName)) { // Only add the column to the table if it does not exist already. // It might exist already if the foreign key is mapped into a regular // property as well. $fieldMapping = $definingClass->getFieldMapping($referencedFieldName); $columnDef = null; if (isset($joinColumn['columnDefinition'])) { $columnDef = $joinColumn['columnDefinition']; } elseif (isset($fieldMapping['columnDefinition'])) { $columnDef = $fieldMapping['columnDefinition']; } $columnOptions = array('notnull' => false, 'columnDefinition' => $columnDef); if (isset($joinColumn['nullable'])) { $columnOptions['notnull'] = !$joinColumn['nullable']; } if (isset($fieldMapping['options'])) { $columnOptions['options'] = $fieldMapping['options']; } if ($fieldMapping['type'] == "string" && isset($fieldMapping['length'])) { $columnOptions['length'] = $fieldMapping['length']; } elseif ($fieldMapping['type'] == "decimal") { $columnOptions['scale'] = $fieldMapping['scale']; $columnOptions['precision'] = $fieldMapping['precision']; } $theJoinTable->addColumn($quotedColumnName, $fieldMapping['type'], $columnOptions); } if (isset($joinColumn['unique']) && $joinColumn['unique'] == true) { $uniqueConstraints[] = array('columns' => array($quotedColumnName)); } if (isset($joinColumn['onDelete'])) { $fkOptions['onDelete'] = $joinColumn['onDelete']; } } // Prefer unique constraints over implicit simple indexes created for foreign keys. // Also avoids index duplication. foreach ($uniqueConstraints as $indexName => $unique) { $theJoinTable->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName); } $compositeName = $theJoinTable->getName() . '.' . implode('', $localColumns); if (isset($addedFks[$compositeName]) && ($foreignTableName != $addedFks[$compositeName]['foreignTableName'] || 0 < count(array_diff($foreignColumns, $addedFks[$compositeName]['foreignColumns'])))) { foreach ($theJoinTable->getForeignKeys() as $fkName => $key) { if (0 === count(array_diff($key->getLocalColumns(), $localColumns)) && ($key->getForeignTableName() != $foreignTableName || 0 < count(array_diff($key->getForeignColumns(), $foreignColumns)))) { $theJoinTable->removeForeignKey($fkName); break; } } $blacklistedFks[$compositeName] = true; } elseif (!isset($blacklistedFks[$compositeName])) { $addedFks[$compositeName] = array('foreignTableName' => $foreignTableName, 'foreignColumns' => $foreignColumns); $theJoinTable->addUnnamedForeignKeyConstraint($foreignTableName, $localColumns, $foreignColumns, $fkOptions); } }
protected function renameManyToOneExtendField(Schema $schema, QueryBag $queries, Table $table, $associationName) { $oldColumnName = 'field_' . $associationName . '_id'; if ($table->hasColumn($oldColumnName)) { $newColumnName = $this->nameGenerator->generateManyToOneRelationColumnName($associationName); $this->renameExtension->renameColumn($schema, $queries, $table, $oldColumnName, $newColumnName); } }
/** * Returns the difference between the tables $table1 and $table2. * * If there are no differences this method returns the boolean false. * * @param \Doctrine\DBAL\Schema\Table $table1 * @param \Doctrine\DBAL\Schema\Table $table2 * * @return boolean|\Doctrine\DBAL\Schema\TableDiff */ public function diffTable(Table $table1, Table $table2) { $changes = 0; $tableDifferences = new TableDiff($table1->getName()); $tableDifferences->fromTable = $table1; $table1Columns = $table1->getColumns(); $table2Columns = $table2->getColumns(); /* See if all the fields in table 1 exist in table 2 */ foreach ($table2Columns as $columnName => $column) { if (!$table1->hasColumn($columnName)) { $tableDifferences->addedColumns[$columnName] = $column; $changes++; } } /* See if there are any removed fields in table 2 */ foreach ($table1Columns as $columnName => $column) { // See if column is removed in table 2. if (!$table2->hasColumn($columnName)) { $tableDifferences->removedColumns[$columnName] = $column; $changes++; continue; } // See if column has changed properties in table 2. $changedProperties = $this->diffColumn($column, $table2->getColumn($columnName)); if (!empty($changedProperties)) { $columnDiff = new ColumnDiff($column->getName(), $table2->getColumn($columnName), $changedProperties); $columnDiff->fromColumn = $column; $tableDifferences->changedColumns[$column->getName()] = $columnDiff; $changes++; } } $this->detectColumnRenamings($tableDifferences); $table1Indexes = $table1->getIndexes(); $table2Indexes = $table2->getIndexes(); /* See if all the indexes in table 1 exist in table 2 */ foreach ($table2Indexes as $indexName => $index) { if ($index->isPrimary() && $table1->hasPrimaryKey() || $table1->hasIndex($indexName)) { continue; } $tableDifferences->addedIndexes[$indexName] = $index; $changes++; } /* See if there are any removed indexes in table 2 */ foreach ($table1Indexes as $indexName => $index) { // See if index is removed in table 2. if ($index->isPrimary() && !$table2->hasPrimaryKey() || !$index->isPrimary() && !$table2->hasIndex($indexName)) { $tableDifferences->removedIndexes[$indexName] = $index; $changes++; continue; } // See if index has changed in table 2. $table2Index = $index->isPrimary() ? $table2->getPrimaryKey() : $table2->getIndex($indexName); if ($this->diffIndex($index, $table2Index)) { $tableDifferences->changedIndexes[$indexName] = $table2Index; $changes++; } } $this->detectIndexRenamings($tableDifferences); $fromFkeys = $table1->getForeignKeys(); $toFkeys = $table2->getForeignKeys(); foreach ($fromFkeys as $key1 => $constraint1) { foreach ($toFkeys as $key2 => $constraint2) { if ($this->diffForeignKey($constraint1, $constraint2) === false) { unset($fromFkeys[$key1]); unset($toFkeys[$key2]); } else { if (strtolower($constraint1->getName()) == strtolower($constraint2->getName())) { $tableDifferences->changedForeignKeys[] = $constraint2; $changes++; unset($fromFkeys[$key1]); unset($toFkeys[$key2]); } } } } foreach ($fromFkeys as $constraint1) { $tableDifferences->removedForeignKeys[] = $constraint1; $changes++; } foreach ($toFkeys as $constraint2) { $tableDifferences->addedForeignKeys[] = $constraint2; $changes++; } return $changes ? $tableDifferences : false; }
/** * @dataProvider getNormalizesAssetNames * @group DBAL-831 */ public function testNormalizesColumnNames($assetName) { $table = new Table('test'); $table->addColumn($assetName, 'integer'); $table->addIndex(array($assetName), $assetName); $table->addForeignKeyConstraint('test', array($assetName), array($assetName), array(), $assetName); $this->assertTrue($table->hasColumn($assetName)); $this->assertTrue($table->hasColumn('foo')); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\Column', $table->getColumn($assetName)); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\Column', $table->getColumn('foo')); $this->assertTrue($table->hasIndex($assetName)); $this->assertTrue($table->hasIndex('foo')); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\Index', $table->getIndex($assetName)); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\Index', $table->getIndex('foo')); $this->assertTrue($table->hasForeignKey($assetName)); $this->assertTrue($table->hasForeignKey('foo')); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\ForeignKeyConstraint', $table->getForeignKey($assetName)); $this->assertInstanceOf('Doctrine\\DBAL\\Schema\\ForeignKeyConstraint', $table->getForeignKey('foo')); $table->renameIndex($assetName, $assetName); $this->assertTrue($table->hasIndex($assetName)); $this->assertTrue($table->hasIndex('foo')); $table->renameIndex($assetName, 'foo'); $this->assertTrue($table->hasIndex($assetName)); $this->assertTrue($table->hasIndex('foo')); $table->renameIndex('foo', $assetName); $this->assertTrue($table->hasIndex($assetName)); $this->assertTrue($table->hasIndex('foo')); $table->renameIndex($assetName, 'bar'); $this->assertFalse($table->hasIndex($assetName)); $this->assertFalse($table->hasIndex('foo')); $this->assertTrue($table->hasIndex('bar')); $table->renameIndex('bar', $assetName); $table->dropColumn($assetName); $table->dropIndex($assetName); $table->removeForeignKey($assetName); $this->assertFalse($table->hasColumn($assetName)); $this->assertFalse($table->hasColumn('foo')); $this->assertFalse($table->hasIndex($assetName)); $this->assertFalse($table->hasIndex('foo')); $this->assertFalse($table->hasForeignKey($assetName)); $this->assertFalse($table->hasForeignKey('foo')); }
/** * Test that a column exists. * * @return $this */ public function exists() { $this->assertTrue($this->table->hasColumn($this->name), "The table column `{$this->name}` does not exist."); return $this; }
/** * Creates the sqlite unit test database */ public function createSqlData() { $dataDir = __DIR__ . '/tests/unit/ORM/data'; $database = $dataDir . '/original/sqlite.test.db'; $this->say('Creating test database in ' . $database); if (file_exists($database)) { $this->say('Removing old test database'); $this->_remove($database); } $connection = DriverManager::getConnection(['url' => 'sqlite:///' . $database]); $files = glob($dataDir . '/original/*.csv'); foreach ($files as $file) { $tableName = basename($file, '.csv'); $table = new Table($tableName); $records = $this->loadData($file); $columns = array_keys(reset($records)); foreach ($columns as $column) { $type = preg_match('~\\bid$~i', $column) ? 'integer' : 'string'; $table->addColumn($column, $type, ['Notnull' => false]); } if ($table->hasColumn('id')) { $table->setPrimaryKey(['id']); } $this->say('Creating table ' . $tableName); $connection->getSchemaManager()->createTable($table); $this->say('Adding ' . count($records) . ' records'); foreach ($records as $record) { $connection->insert($tableName, $record); } } }