本文整理汇总了PHP中Doctrine\DBAL\Schema\Table::addForeignKeyConstraint方法的典型用法代码示例。如果您正苦于以下问题:PHP Table::addForeignKeyConstraint方法的具体用法?PHP Table::addForeignKeyConstraint怎么用?PHP Table::addForeignKeyConstraint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\DBAL\Schema\Table
的用法示例。
在下文中一共展示了Table::addForeignKeyConstraint方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: map
/**
* @param string $schema
* @param Table $table
* @param string $name
* @param FieldDefinition $definition
*
* @throws DoctrineStorageException
*/
public function map(string $schema, Table $table, string $name, FieldDefinition $definition)
{
if (!$definition instanceof AssociationFieldDefinition) {
throw DoctrineStorageException::invalidDefinition(AssociationFieldDefinition::class, $definition);
}
$table->addColumn($name, 'guid', ['notnull' => !$definition->isNullable(), 'default' => $definition->defaultValue(), 'length' => $definition->options()['length'] ?? null, 'unique' => $definition->options()['unique'] ?? false]);
$table->addForeignKeyConstraint($this->tableName($schema, $definition->typeSchema()->name()), [$name], ['id']);
}
示例2: addForeignKey
/**
* @param DBALTable $KeyTarget Foreign Key (Column: KeySource Name)
* @param DBALTable $KeySource Foreign Data (Column: Id)
*/
public function addForeignKey(DBALTable &$KeyTarget, DBALTable $KeySource)
{
if (!$this->Database->hasColumn($KeyTarget->getName(), $KeySource->getName())) {
$KeyTarget->addColumn($KeySource->getName(), 'bigint');
if ($this->Database->getPlatform()->supportsForeignKeyConstraints()) {
$KeyTarget->addForeignKeyConstraint($KeySource, array($KeySource->getName()), array('Id'));
}
}
}
示例3: doIt
public function doIt(Table $table, Schema $schema = null)
{
$table->addColumn("title", "string", ['notnull' => false]);
$table->addColumn("sort", "integer", ['notnull' => false]);
$table->addColumn("path", "string", ['notnull' => false]);
$table->addColumn("level", "integer", ['notnull' => false]);
$table->addColumn("parent_id", "integer", ['notnull' => false]);
$table->addForeignKeyConstraint($table, array('parent_id'), array("id"), array("onUpdate" => "CASCADE"));
}
示例4: setOwnerCascadeDelete
/**
* @param Schema $schema
* @param Table $table
* @param string $keyName
*/
protected function setOwnerCascadeDelete(Schema $schema, Table $table, $keyName)
{
foreach ($table->getForeignKeys() as $foreignKey) {
if ($foreignKey->getLocalColumns() == ['owner_id']) {
$table->removeForeignKey($foreignKey->getName());
$table->addForeignKeyConstraint($schema->getTable('orocrm_contact'), ['owner_id'], ['id'], ['onDelete' => 'CASCADE', 'onUpdate' => null], $keyName);
break;
}
}
}
示例5: testEncodedForeignKeyConstraintNameIsTheSameAsDoctrineDefault
public function testEncodedForeignKeyConstraintNameIsTheSameAsDoctrineDefault()
{
$tableName1 = 'tbl123456789012345';
$columnName1 = 'clmn1234567890';
$tableName2 = 'tbl1234567890';
$columnName2 = 'clmn12345';
$table1 = new Table($tableName1, [new Column($columnName1, Type::getType('integer'))]);
$table2 = new Table($tableName2, [new Column($columnName2, Type::getType('integer'))]);
$table2->setPrimaryKey([$columnName2]);
$table1->addForeignKeyConstraint($table2, [$columnName1], [$columnName2]);
$foreignKeys = $table1->getForeignKeys();
$doctrineResult = array_pop($foreignKeys)->getName();
$generator = new DbIdentifierNameGenerator();
$result = $generator->generateForeignKeyConstraintName($tableName1, [$columnName1]);
$this->assertEquals($doctrineResult, $result);
}
示例6: resetDatabase
public function resetDatabase()
{
$dbPath = $this->app['sqlite_path'];
$dbDir = dirname($dbPath);
$filesystem = new Filesystem();
$filesystem->mkdir($dbDir);
$filesystem->chmod($dbDir, 0777, 00, true);
if (!is_writable($dbDir)) {
throw new \Exception('Unable to write to ' . $dbPath);
}
$schemaManager = $this->getConnection()->getSchemaManager();
$userTable = new Table('user');
$userTable->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
$userTable->setPrimaryKey(array('id'));
$userTable->addColumn('name', 'string', array('length' => 255));
$userTable->addUniqueIndex(array('name'));
$userTable->addColumn('age', 'integer');
$schemaManager->dropAndCreateTable($userTable);
$bookTable = new Table('book');
$bookTable->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
$bookTable->setPrimaryKey(array('id'));
$bookTable->addColumn('name', 'string', array('length' => 255));
$bookTable->addUniqueIndex(array('name'));
$bookTable->addColumn('userID', 'integer');
$bookTable->addColumn('ISBN', 'integer');
$bookTable->addForeignKeyConstraint($userTable, array('userID'), array('id'));
$schemaManager->dropAndCreateTable($bookTable);
$bannedTable = new Table('banned');
$bannedTable->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
$bannedTable->setPrimaryKey(array('id'));
$bannedTable->addColumn('name', 'string', array('length' => 255));
$bannedTable->addUniqueIndex(array('name'));
$bannedTable->addColumn('userID', 'integer');
$bannedTable->addColumn('ISBN', 'integer');
$schemaManager->dropAndCreateTable($bannedTable);
}
示例7: addForeign
public function addForeign(Table $table, Table $foreignTable)
{
$columnName = $foreignTable->getName() . '_id';
$table->addColumn($columnName, "integer", ['notnull' => false]);
$table->addForeignKeyConstraint($foreignTable, array($columnName), array("id"), array("onUpdate" => "CASCADE", "onDelete" => "SET NULL"));
}
示例8: testDoesNotListIndexesImplicitlyCreatedByForeignKeys
/**
* @group DBAL-1095
*/
public function testDoesNotListIndexesImplicitlyCreatedByForeignKeys()
{
if (!$this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
$this->markTestSkipped('This test is only supported on platforms that have foreign keys.');
}
$primaryTable = new Table('test_list_index_implicit_primary');
$primaryTable->addColumn('id', 'integer');
$primaryTable->setPrimaryKey(array('id'));
$foreignTable = new Table('test_list_index_implicit_foreign');
$foreignTable->addColumn('fk1', 'integer');
$foreignTable->addColumn('fk2', 'integer');
$foreignTable->addIndex(array('fk1'), 'explicit_fk1_idx');
$foreignTable->addForeignKeyConstraint('test_list_index_implicit_primary', array('fk1'), array('id'));
$foreignTable->addForeignKeyConstraint('test_list_index_implicit_primary', array('fk2'), array('id'));
$this->_sm->dropAndCreateTable($primaryTable);
$this->_sm->dropAndCreateTable($foreignTable);
$indexes = $this->_sm->listTableIndexes('test_list_index_implicit_foreign');
$this->assertCount(2, $indexes);
$this->assertArrayHasKey('explicit_fk1_idx', $indexes);
$this->assertArrayHasKey('idx_6d88c7b4fdc58d6c', $indexes);
}
示例9: foreign
/**
* Specify a foreign key for the table.
*
* @param string $table
* @param array|string $localColumnNames
* @param array|string $foreignColumnNames
* @param array $options
* @param null $constraintName
*
* @return Blueprint
*/
public function foreign($table, $localColumnNames, $foreignColumnNames = 'id', $options = [], $constraintName = null)
{
$localColumnNames = is_array($localColumnNames) ? $localColumnNames : [$localColumnNames];
$foreignColumnNames = is_array($foreignColumnNames) ? $foreignColumnNames : [$foreignColumnNames];
return $this->table->addForeignKeyConstraint($table, $localColumnNames, $foreignColumnNames, $options, $constraintName);
}
示例10: resetDatabase
public function resetDatabase()
{
// 1) check DB permissions
$dbPath = $this->app['sqlite_path'];
$dbDir = dirname($dbPath);
// make sure the directory is available and writeable
$filesystem = new Filesystem();
$filesystem->mkdir($dbDir);
$filesystem->chmod($dbDir, 0777, 00, true);
if (!is_writable($dbDir)) {
throw new \Exception('Unable to write to ' . $dbPath);
}
// 2) Add some tables bro!
$schemaManager = $this->getConnection()->getSchemaManager();
$userTable = new Table('user');
$userTable->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
$userTable->setPrimaryKey(array('id'));
$userTable->addColumn('email', 'string', array('length' => 255));
$userTable->addUniqueIndex(array('email'));
$userTable->addColumn('username', 'string', array('length' => 50));
$userTable->addUniqueIndex(array('username'));
$userTable->addColumn('password', 'string', array('length' => 255));
$schemaManager->dropAndCreateTable($userTable);
$tokenTable = new Table(ApiTokenRepository::TABLE_NAME);
$tokenTable->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
$tokenTable->setPrimaryKey(array('id'));
$tokenTable->addColumn('token', 'string', array('length' => 32));
$tokenTable->addColumn('userId', 'integer');
$tokenTable->addColumn('notes', 'string', array('length' => 255));
$tokenTable->addColumn('createdAt', 'datetime');
$tokenTable->addUniqueIndex(array('token'), 'token_idx');
$tokenTable->addForeignKeyConstraint($userTable, array('userId'), array('id'));
$schemaManager->dropAndCreateTable($tokenTable);
$programmerTable = new Table('programmer');
$programmerTable->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
$programmerTable->setPrimaryKey(array('id'));
$programmerTable->addColumn('nickname', 'string', array('length' => 255));
$programmerTable->addUniqueIndex(array('nickname'));
$programmerTable->addColumn('avatarNumber', 'integer');
$programmerTable->addColumn('tagLine', 'integer', array('notnull' => false));
$programmerTable->addColumn('userId', 'integer');
$programmerTable->addColumn('powerLevel', 'integer');
$programmerTable->addForeignKeyConstraint($userTable, array('userId'), array('id'));
$schemaManager->dropAndCreateTable($programmerTable);
$projectTable = new Table('project');
$projectTable->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
$projectTable->setPrimaryKey(array('id'));
$projectTable->addColumn('name', 'string', array('length' => 255));
$projectTable->addColumn('difficultyLevel', 'integer');
$schemaManager->dropAndCreateTable($projectTable);
$battleTable = new Table('battle');
$battleTable->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
$battleTable->setPrimaryKey(array('id'));
$battleTable->addColumn('programmerId', 'integer');
$battleTable->addColumn('projectId', 'integer');
$battleTable->addColumn('didProgrammerWin', 'integer');
$battleTable->addColumn('foughtAt', 'datetime');
$battleTable->addColumn('notes', 'text');
$battleTable->addForeignKeyConstraint($programmerTable, array('programmerId'), array('id'));
$battleTable->addForeignKeyConstraint($projectTable, array('projectId'), array('id'));
$schemaManager->dropAndCreateTable($battleTable);
}
示例11: testTableUpdateForeignKey
public function testTableUpdateForeignKey()
{
$tableForeign = new Table("bar");
$tableForeign->addColumn('id', 'integer');
$table1 = new Table("foo");
$table1->addColumn('fk', 'integer');
$table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
$table2 = new Table("foo");
$table2->addColumn('fk', 'integer');
$table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'), array('onUpdate' => 'CASCADE'));
$c = new Comparator();
$tableDiff = $c->diffTable($table1, $table2);
$this->assertType('Doctrine\\DBAL\\Schema\\TableDiff', $tableDiff);
$this->assertEquals(1, count($tableDiff->changedForeignKeys));
}
示例12: testAddForeignKeyConstraint
public function testAddForeignKeyConstraint()
{
$table = new Table("foo");
$table->addColumn("id", 'integer');
$foreignTable = new Table("bar");
$foreignTable->addColumn("id", 'integer');
$table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
$constraints = $table->getForeignKeys();
$this->assertEquals(1, count($constraints));
$this->assertType('Doctrine\\DBAL\\Schema\\ForeignKeyConstraint', $constraints["foo_id_fk"]);
$this->assertEquals("foo_id_fk", $constraints["foo_id_fk"]->getName());
$this->assertTrue($constraints["foo_id_fk"]->hasOption("foo"));
$this->assertEquals("bar", $constraints["foo_id_fk"]->getOption("foo"));
}
示例13: parseForeignKey
protected static function parseForeignKey(Schema $schema, Table $table, SimpleXMLElement $xForeignKey, AbstractPlatform $platform)
{
$foreignTable = (string) $xForeignKey['table'];
$localColumnNames = array();
$foreignColumnNames = array();
foreach ($xForeignKey->column as $xColumn) {
$localColumnNames[] = (string) $xColumn['local'];
$foreignColumnNames[] = (string) $xColumn['foreign'];
}
$constraintName = isset($xForeignKey['name']) ? (string) $xForeignKey['name'] : null;
$options = array();
if (isset($xForeignKey['onupdate'])) {
$options['onUpdate'] = (string) $xForeignKey['onupdate'];
}
if (isset($xForeignKey['ondelete'])) {
$options['onDelete'] = (string) $xForeignKey['ondelete'];
}
$table->addForeignKeyConstraint($foreignTable, $localColumnNames, $foreignColumnNames, $options, $constraintName);
}
示例14: setUpForeignKeyConstraintViolationExceptionTest
private function setUpForeignKeyConstraintViolationExceptionTest()
{
$schemaManager = $this->_conn->getSchemaManager();
$table = new Table("constraint_error_table");
$table->addColumn('id', 'integer', array());
$table->setPrimaryKey(array('id'));
$owningTable = new Table("owning_table");
$owningTable->addColumn('id', 'integer', array());
$owningTable->addColumn('constraint_id', 'integer', array());
$owningTable->setPrimaryKey(array('id'));
$owningTable->addForeignKeyConstraint($table, array('constraint_id'), array('id'));
$schemaManager->createTable($table);
$schemaManager->createTable($owningTable);
}
示例15: testQuotedColumnInForeignKeyPropagation
/**
* @group DBAL-374
*/
public function testQuotedColumnInForeignKeyPropagation()
{
$table = new Table('`quoted`');
$table->addColumn('create', 'string');
$table->addColumn('foo', 'string');
$table->addColumn('`bar`', 'string');
// Foreign table with reserved keyword as name (needs quotation).
$foreignTable = new Table('foreign');
$foreignTable->addColumn('create', 'string');
// Foreign column with reserved keyword as name (needs quotation).
$foreignTable->addColumn('bar', 'string');
// Foreign column with non-reserved keyword as name (does not need quotation).
$foreignTable->addColumn('`foo-bar`', 'string');
// Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
$table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_RESERVED_KEYWORD');
// Foreign table with non-reserved keyword as name (does not need quotation).
$foreignTable = new Table('foo');
$foreignTable->addColumn('create', 'string');
// Foreign column with reserved keyword as name (needs quotation).
$foreignTable->addColumn('bar', 'string');
// Foreign column with non-reserved keyword as name (does not need quotation).
$foreignTable->addColumn('`foo-bar`', 'string');
// Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
$table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_NON_RESERVED_KEYWORD');
// Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
$foreignTable = new Table('`foo-bar`');
$foreignTable->addColumn('create', 'string');
// Foreign column with reserved keyword as name (needs quotation).
$foreignTable->addColumn('bar', 'string');
// Foreign column with non-reserved keyword as name (does not need quotation).
$foreignTable->addColumn('`foo-bar`', 'string');
// Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
$table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_INTENDED_QUOTATION');
$sql = $this->_platform->getCreateTableSQL($table, AbstractPlatform::CREATE_FOREIGNKEYS);
$this->assertEquals($this->getQuotedColumnInForeignKeySQL(), $sql);
}