本文整理汇总了PHP中Propel\Generator\Model\ForeignKey::setOnUpdate方法的典型用法代码示例。如果您正苦于以下问题:PHP ForeignKey::setOnUpdate方法的具体用法?PHP ForeignKey::setOnUpdate怎么用?PHP ForeignKey::setOnUpdate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Propel\Generator\Model\ForeignKey
的用法示例。
在下文中一共展示了ForeignKey::setOnUpdate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: relateI18nTableToMainTable
protected function relateI18nTableToMainTable()
{
$table = $this->getTable();
$i18nTable = $this->i18nTable;
$pks = $this->getTable()->getPrimaryKey();
if (count($pks) > 1) {
throw new EngineException('The i18n behavior does not support tables with composite primary keys');
}
foreach ($pks as $column) {
if (!$i18nTable->hasColumn($column->getName())) {
$column = clone $column;
$column->setAutoIncrement(false);
$i18nTable->addColumn($column);
}
}
if (in_array($table->getName(), $i18nTable->getForeignTableNames())) {
return;
}
$fk = new ForeignKey();
$fk->setForeignTableCommonName($table->getCommonName());
$fk->setForeignSchemaName($table->getSchema());
$fk->setDefaultJoin('LEFT JOIN');
$fk->setOnDelete(ForeignKey::CASCADE);
$fk->setOnUpdate(ForeignKey::NONE);
foreach ($pks as $column) {
$fk->addReference($column->getName(), $column->getName());
}
$i18nTable->addForeignKey($fk);
}
示例2: addClosureColumn
protected function addClosureColumn($name, Table $ct_table, Column $column)
{
$table = $this->getTable();
$id_fieldname = $column->getName();
$domain = $column->getDomain();
if (!$ct_table->hasColumn($name)) {
$column = new Column($name);
$column->setDomain($domain);
$column->setPrimaryKey(true);
$ct_table->addColumn($column);
} else {
$column = $ct_table->getColumn($name);
}
$ct_tablename_normalized = str_replace('_', '', $ct_table->getName());
$fk_name = $ct_tablename_normalized . '_' . $name . '_fk';
if (!$ct_table->getColumnForeignKeys($name)) {
$column_fk = new ForeignKey($fk_name);
$column_fk->addReference($name, $table->getColumn($id_fieldname)->getName());
$column_fk->setForeignTableCommonName($table->getName());
$column_fk->setOnUpdate('cascade');
$column_fk->setOnDelete('restrict');
$ct_table->addForeignKey($column_fk);
}
$column_idx_name = $fk_name . '_idx';
if (!$ct_table->hasIndex($column_idx_name)) {
$column_idx = new Index($column_idx_name);
$column_idx->addColumn(['name' => $column->getName()]);
$ct_table->addIndex($column_idx);
}
}
示例3: modifyTable
/**
* Adds all columns, indexes, constraints and additional tables.
*/
public function modifyTable()
{
$table = $this->getTable();
$tableName = $table->getName();
$foreignTableName = $this->getForeignTable();
// enable reload on insert to force the model to load the trigger generated id(s)
$table->setReloadOnInsert(true);
$foreignIdColumnName = $foreignTableName . '_id';
$compositeKeyColumnName = $foreignTableName . '_' . $tableName . '_id';
if ($table->hasBehavior('concrete_inheritance')) {
// we're a child in a concrete inheritance
$parentTableName = $table->getBehavior('concrete_inheritance')->getParameter('extends');
$parentTable = $table->getDatabase()->getTable($parentTableName);
if ($parentTable->hasBehavior('\\' . __CLASS__)) {
//we're a child of a concrete inheritance structure, so we're going to skip this
//round here because this behavior has also been attached by the parent table.
return;
}
}
if ($table->hasColumn($foreignIdColumnName)) {
$foreignIdColumn = $table->getColumn($foreignIdColumnName);
} else {
$foreignIdColumn = $table->addColumn(array('name' => $foreignIdColumnName, 'type' => 'integer', 'required' => true));
$compositeKeyForeignKeyName = $tableName . '_FK_' . $foreignIdColumnName;
$foreignKey = new ForeignKey($compositeKeyForeignKeyName);
$foreignKey->addReference($foreignIdColumnName, 'id');
$foreignKey->setForeignTableCommonName($foreignTableName);
$foreignKey->setOnUpdate(ForeignKey::CASCADE);
$foreignKey->setOnDelete(ForeignKey::CASCADE);
$table->addForeignKey($foreignKey);
}
if ($table->hasColumn($compositeKeyColumnName)) {
$compositeKeyColumn = $table->getColumn($compositeKeyColumnName);
} else {
$compositeKeyColumn = $table->addColumn(array('name' => $compositeKeyColumnName, 'type' => 'integer', 'required' => false));
}
$index = new Unique($tableName . '_UQ_' . $foreignIdColumnName . '_' . $compositeKeyColumnName);
$index->addColumn($foreignIdColumn);
$index->addColumn($compositeKeyColumn);
$table->addUnique($index);
$database = $table->getDatabase();
$sequenceTableName = sprintf('%s_sequence', $foreignTableName);
if (!$database->hasTable($sequenceTableName)) {
$sequenceTable = $database->addTable(array('name' => $sequenceTableName, 'package' => $table->getPackage(), 'schema' => $table->getSchema(), 'namespace' => $table->getNamespace() ? '\\' . $table->getNamespace() : null, 'skipSql' => $table->isSkipSql()));
$sequenceTable->addColumn(array('name' => 'table_name', 'type' => 'varchar', 'size' => 32, 'required' => true, 'primaryKey' => true));
$sequenceTable->addColumn(array('name' => $foreignIdColumnName, 'type' => 'integer', 'required' => true, 'primaryKey' => true));
$sequenceTable->addColumn(array('name' => $foreignTableName . '_max_sequence_id', 'type' => 'integer', 'required' => false, 'default' => null));
}
}
示例4: relateDelegateToMainTable
protected function relateDelegateToMainTable($delegateTable, $mainTable)
{
$pks = $mainTable->getPrimaryKey();
foreach ($pks as $column) {
$mainColumnName = $column->getName();
if (!$delegateTable->hasColumn($mainColumnName)) {
$column = clone $column;
$column->setAutoIncrement(false);
$delegateTable->addColumn($column);
}
}
// Add a one-to-one fk
$fk = new ForeignKey();
$fk->setForeignTableCommonName($mainTable->getCommonName());
$fk->setForeignSchemaName($mainTable->getSchema());
$fk->setDefaultJoin('LEFT JOIN');
$fk->setOnDelete(ForeignKey::CASCADE);
$fk->setOnUpdate(ForeignKey::NONE);
foreach ($pks as $column) {
$fk->addReference($column->getName(), $column->getName());
}
$delegateTable->addForeignKey($fk);
}
示例5: addForeignKeys
protected function addForeignKeys(Table $table)
{
$database = $table->getDatabase();
$stmt = $this->dbh->query('PRAGMA foreign_key_list("' . $table->getName() . '")');
$lastId = null;
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
if ($lastId !== $row['id']) {
$fk = new ForeignKey();
$onDelete = $row['on_delete'];
if ($onDelete && 'NO ACTION' !== $onDelete) {
$fk->setOnDelete($onDelete);
}
$onUpdate = $row['on_update'];
if ($onUpdate && 'NO ACTION' !== $onUpdate) {
$fk->setOnUpdate($onUpdate);
}
$foreignTable = $database->getTable($row['table'], true);
if (!$foreignTable) {
continue;
}
$table->addForeignKey($fk);
$fk->setForeignTableCommonName($foreignTable->getCommonName());
if ($table->guessSchemaName() != $foreignTable->guessSchemaName()) {
$fk->setForeignSchemaName($foreignTable->guessSchemaName());
}
$lastId = $row['id'];
}
$fk->addReference($row['from'], $row['to']);
}
}
示例6: addForeignKeys
/**
* Load foreign keys for this table.
*
* @param Table $table The Table model class to add FKs to
*/
protected function addForeignKeys(Table $table)
{
// local store to avoid duplicates
$foreignKeys = array();
/* @var StatementInterface $stmt */
$stmt = $this->dbh->query("SELECT CONSTRAINT_NAME, DELETE_RULE, R_CONSTRAINT_NAME FROM USER_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'R' AND TABLE_NAME = '" . $table->getName() . "'");
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
// Local reference
/* @var StatementInterface $stmt2 */
$stmt2 = $this->dbh->query("SELECT COLUMN_NAME FROM USER_CONS_COLUMNS WHERE CONSTRAINT_NAME = '" . $row['CONSTRAINT_NAME'] . "' AND TABLE_NAME = '" . $table->getName() . "'");
$localReferenceInfo = $stmt2->fetch(\PDO::FETCH_ASSOC);
// Foreign reference
$stmt2 = $this->dbh->query("SELECT TABLE_NAME, COLUMN_NAME FROM USER_CONS_COLUMNS WHERE CONSTRAINT_NAME = '" . $row['R_CONSTRAINT_NAME'] . "'");
$foreignReferenceInfo = $stmt2->fetch(\PDO::FETCH_ASSOC);
if (!isset($foreignKeys[$row['CONSTRAINT_NAME']])) {
$fk = new ForeignKey($row['CONSTRAINT_NAME']);
$fk->setForeignTableCommonName($foreignReferenceInfo['TABLE_NAME']);
$onDelete = 'NO ACTION' === $row['DELETE_RULE'] ? 'NONE' : $row['DELETE_RULE'];
$fk->setOnDelete($onDelete);
$fk->setOnUpdate($onDelete);
$fk->addReference(array('local' => $localReferenceInfo['COLUMN_NAME'], 'foreign' => $foreignReferenceInfo['COLUMN_NAME']));
$table->addForeignKey($fk);
$foreignKeys[$row['CONSTRAINT_NAME']] = $fk;
}
}
}
示例7: modifyTable
public function modifyTable()
{
$table = $this->getTable();
$parentTable = $this->getParentTable();
if ($this->isCopyData()) {
// tell the parent table that it has a descendant
if (!$parentTable->hasBehavior('concrete_inheritance_parent')) {
$parentBehavior = new ConcreteInheritanceParentBehavior();
$parentBehavior->setName('concrete_inheritance_parent');
$parentBehavior->addParameter(array('name' => 'descendant_column', 'value' => $this->getParameter('descendant_column')));
$parentTable->addBehavior($parentBehavior);
// The parent table's behavior modifyTable() must be executed before this one
$parentBehavior->getTableModifier()->modifyTable();
$parentBehavior->setTableModified(true);
}
}
// Add the columns of the parent table
foreach ($parentTable->getColumns() as $column) {
if ($column->getName() == $this->getParameter('descendant_column')) {
continue;
}
if ($table->hasColumn($column->getName())) {
continue;
}
$copiedColumn = clone $column;
if ($column->isAutoIncrement() && $this->isCopyData()) {
$copiedColumn->setAutoIncrement(false);
}
$table->addColumn($copiedColumn);
if ($column->isPrimaryKey() && $this->isCopyData()) {
$fk = new ForeignKey();
$fk->setForeignTableCommonName($column->getTable()->getCommonName());
$fk->setForeignSchemaName($column->getTable()->getSchema());
$fk->setOnDelete('CASCADE');
$fk->setOnUpdate(null);
$fk->addReference($copiedColumn, $column);
$fk->isParentChild = true;
$table->addForeignKey($fk);
}
}
// add the foreign keys of the parent table
foreach ($parentTable->getForeignKeys() as $fk) {
$copiedFk = clone $fk;
$copiedFk->setName('');
$copiedFk->setRefPhpName('');
$this->getTable()->addForeignKey($copiedFk);
}
// add the indices of the parent table
foreach ($parentTable->getIndices() as $index) {
$copiedIndex = clone $index;
$copiedIndex->setName('');
$this->getTable()->addIndex($copiedIndex);
}
// add the unique indices of the parent table
foreach ($parentTable->getUnices() as $unique) {
$copiedUnique = clone $unique;
$copiedUnique->setName('');
$this->getTable()->addUnique($copiedUnique);
}
// add the Behaviors of the parent table
foreach ($parentTable->getBehaviors() as $behavior) {
if ($behavior->getName() == 'concrete_inheritance_parent' || $behavior->getName() == 'concrete_inheritance') {
continue;
}
//validate behavior. If validate behavior already exists, clone only rules from parent
if ('validate' === $behavior->getName() && $table->hasBehavior('validate')) {
$table->getBehavior('validate')->mergeParameters($behavior->getParameters());
continue;
}
$copiedBehavior = clone $behavior;
$copiedBehavior->setTableModified(false);
$this->getTable()->addBehavior($copiedBehavior);
}
}
示例8: addVersionTable
protected function addVersionTable()
{
$table = $this->getTable();
$database = $table->getDatabase();
$versionTableName = $this->getParameter('version_table') ? $this->getParameter('version_table') : $table->getName() . '_version';
if (!$database->hasTable($versionTableName)) {
// create the version table
$versionTable = $database->addTable(array('name' => $versionTableName, 'phpName' => $this->getVersionTablePhpName(), 'package' => $table->getPackage(), 'schema' => $table->getSchema(), 'namespace' => $table->getNamespace() ? '\\' . $table->getNamespace() : null, 'skipSql' => $table->isSkipSql()));
$versionTable->isVersionTable = true;
// every behavior adding a table should re-execute database behaviors
foreach ($database->getBehaviors() as $behavior) {
$behavior->modifyDatabase();
}
// copy all the columns
foreach ($table->getColumns() as $column) {
$columnInVersionTable = clone $column;
$columnInVersionTable->clearInheritanceList();
if ($columnInVersionTable->hasReferrers()) {
$columnInVersionTable->clearReferrers();
}
if ($columnInVersionTable->isAutoincrement()) {
$columnInVersionTable->setAutoIncrement(false);
}
$versionTable->addColumn($columnInVersionTable);
}
// create the foreign key
$fk = new ForeignKey();
$fk->setForeignTableCommonName($table->getCommonName());
$fk->setForeignSchemaName($table->getSchema());
$fk->setOnDelete('CASCADE');
$fk->setOnUpdate(null);
$tablePKs = $table->getPrimaryKey();
foreach ($versionTable->getPrimaryKey() as $key => $column) {
$fk->addReference($column, $tablePKs[$key]);
}
$versionTable->addForeignKey($fk);
// add the version column to the primary key
$versionColumn = $versionTable->getColumn($this->getParameter('version_column'));
$versionColumn->setNotNull(true);
$versionColumn->setPrimaryKey(true);
$this->versionTable = $versionTable;
} else {
$this->versionTable = $database->getTable($versionTableName);
}
}
示例9: addForeignKeys
/**
* Load foreign keys for this table.
*/
protected function addForeignKeys(Table $table, $oid)
{
$database = $table->getDatabase();
$stmt = $this->dbh->prepare("SELECT\n conname,\n confupdtype,\n confdeltype,\n CASE nl.nspname WHEN 'public' THEN cl.relname ELSE nl.nspname||'.'||cl.relname END as fktab,\n array_agg(DISTINCT a2.attname) AS fkcols,\n CASE nr.nspname WHEN 'public' THEN cr.relname ELSE nr.nspname||'.'||cr.relname END as reftab,\n array_agg(DISTINCT a1.attname) AS refcols\n FROM pg_constraint ct\n JOIN pg_class cl ON cl.oid=conrelid\n JOIN pg_class cr ON cr.oid=confrelid\n JOIN pg_namespace nl ON nl.oid = cl.relnamespace\n JOIN pg_namespace nr ON nr.oid = cr.relnamespace\n LEFT JOIN pg_catalog.pg_attribute a1 ON a1.attrelid = ct.confrelid\n LEFT JOIN pg_catalog.pg_attribute a2 ON a2.attrelid = ct.conrelid\n WHERE\n contype='f'\n AND conrelid = ?\n AND a2.attnum = ANY (ct.conkey)\n AND a1.attnum = ANY (ct.confkey)\n GROUP BY conname, confupdtype, confdeltype, fktab, reftab\n ORDER BY conname");
$stmt->bindValue(1, $oid);
$stmt->execute();
$foreignKeys = array();
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$name = $row['conname'];
$localTable = $row['fktab'];
$localColumns = explode(',', trim($row['fkcols'], '{}'));
$foreignTableName = $row['reftab'];
$foreignColumns = explode(',', trim($row['refcols'], '{}'));
// On Update
switch ($row['confupdtype']) {
case 'c':
$onupdate = ForeignKey::CASCADE;
break;
case 'd':
$onupdate = ForeignKey::SETDEFAULT;
break;
case 'n':
$onupdate = ForeignKey::SETNULL;
break;
case 'r':
$onupdate = ForeignKey::RESTRICT;
break;
default:
case 'a':
// NOACTION is the postgresql default
$onupdate = ForeignKey::NONE;
break;
}
// On Delete
switch ($row['confdeltype']) {
case 'c':
$ondelete = ForeignKey::CASCADE;
break;
case 'd':
$ondelete = ForeignKey::SETDEFAULT;
break;
case 'n':
$ondelete = ForeignKey::SETNULL;
break;
case 'r':
$ondelete = ForeignKey::RESTRICT;
break;
default:
case 'a':
// NOACTION is the postgresql default
$ondelete = ForeignKey::NONE;
break;
}
$foreignTable = $database->getTable($foreignTableName);
$localTable = $database->getTable($localTable);
if (!$foreignTable) {
continue;
}
if (!isset($foreignKeys[$name])) {
$fk = new ForeignKey($name);
$fk->setForeignTableCommonName($foreignTable->getCommonName());
if ($table->guessSchemaName() != $foreignTable->guessSchemaName()) {
$fk->setForeignSchemaName($foreignTable->getSchema());
}
$fk->setOnDelete($ondelete);
$fk->setOnUpdate($onupdate);
$table->addForeignKey($fk);
$foreignKeys[$name] = $fk;
}
$max = count($localColumns);
for ($i = 0; $i < $max; $i++) {
$foreignKeys[$name]->addReference($localTable->getColumn($localColumns[$i]), $foreignTable->getColumn($foreignColumns[$i]));
}
}
}
示例10: testCompareOnUpdate
public function testCompareOnUpdate()
{
$c1 = new Column('Foo');
$c2 = new Column('Bar');
$fk1 = new ForeignKey();
$fk1->addReference($c1, $c2);
$fk1->setOnUpdate(ForeignKey::SETNULL);
$t1 = new Table('Baz');
$t1->addForeignKey($fk1);
$c3 = new Column('Foo');
$c4 = new Column('Bar');
$fk2 = new ForeignKey();
$fk2->addReference($c3, $c4);
$fk2->setOnUpdate(ForeignKey::RESTRICT);
$t2 = new Table('Baz');
$t2->addForeignKey($fk2);
$this->assertTrue(PropelForeignKeyComparator::computeDiff($fk1, $fk2));
}
示例11: addSnapshotTable
private function addSnapshotTable()
{
$table = $this->getTable();
$primaryKeyColumn = $table->getFirstPrimaryKeyColumn();
$database = $table->getDatabase();
$snapshotTableName = $this->getParameter(self::PARAMETER_SNAPSHOT_TABLE) ?: $this->getDefaultSnapshotTableName();
if ($database->hasTable($snapshotTableName)) {
$this->snapshotTable = $database->getTable($snapshotTableName);
return;
}
$snapshotTable = $database->addTable(['name' => $snapshotTableName, 'phpName' => $this->getParameter(self::PARAMETER_SNAPSHOT_PHPNAME), 'package' => $table->getPackage(), 'schema' => $table->getSchema(), 'namespace' => $table->getNamespace() ? '\\' . $table->getNamespace() : null]);
$addSnapshotAt = true;
$hasTimestampableBehavior = 0 < count(array_filter($database->getBehaviors(), function (Behavior $behavior) {
return 'timestampable' === $behavior->getName();
}));
if ($hasTimestampableBehavior) {
$addSnapshotAt = false;
$timestampableBehavior = clone $database->getBehavior('timestampable');
$timestampableBehavior->setParameters(array_merge($timestampableBehavior->getParameters(), ['create_column' => $this->getParameter(self::PARAMETER_SNAPSHOT_AT_COLUMN), 'disable_updated_at' => 'true']));
$snapshotTable->addBehavior($timestampableBehavior);
}
$snapshotTable->isSnapshotTable = true;
$idColumn = $snapshotTable->addColumn(['name' => 'id', 'type' => 'INTEGER']);
$idColumn->setAutoIncrement(true);
$idColumn->setPrimaryKey(true);
$idColumn->setNotNull(true);
$columns = $table->getColumns();
foreach ($columns as $column) {
if ($column->isPrimaryKey()) {
continue;
}
$columnInSnapshotTable = clone $column;
$columnInSnapshotTable->setNotNull(false);
if ($columnInSnapshotTable->hasReferrers()) {
$columnInSnapshotTable->clearReferrers();
}
if ($columnInSnapshotTable->isAutoincrement()) {
$columnInSnapshotTable->setAutoIncrement(false);
}
$snapshotTable->addColumn($columnInSnapshotTable);
}
$foreignKeyColumn = $snapshotTable->addColumn(['name' => $this->getParameter(self::PARAMETER_REFERENCE_COLUMN), 'type' => $primaryKeyColumn->getType(), 'size' => $primaryKeyColumn->getSize()]);
$index = new Index();
$index->setName($this->getParameter(self::PARAMETER_REFERENCE_COLUMN));
if ($primaryKeyColumn->getSize()) {
$index->addColumn(['name' => $this->getParameter(self::PARAMETER_REFERENCE_COLUMN), 'size' => $primaryKeyColumn->getSize()]);
} else {
$index->addColumn(['name' => $this->getParameter(self::PARAMETER_REFERENCE_COLUMN)]);
}
$snapshotTable->addIndex($index);
$foreignKey = new ForeignKey();
$foreignKey->setName(vsprintf('fk_%s_%s', [$snapshotTable->getOriginCommonName(), $this->getParameter(self::PARAMETER_REFERENCE_COLUMN)]));
$foreignKey->setOnUpdate('CASCADE');
$foreignKey->setOnDelete('SET NULL');
$foreignKey->setForeignTableCommonName($this->getTable()->getCommonName());
$foreignKey->addReference($foreignKeyColumn, $primaryKeyColumn);
$snapshotTable->addForeignKey($foreignKey);
if ($this->getParameter(self::PARAMETER_LOG_SNAPSHOT_AT) == 'true' && $addSnapshotAt) {
$snapshotTable->addColumn(['name' => $this->getParameter(self::PARAMETER_SNAPSHOT_AT_COLUMN), 'type' => 'TIMESTAMP']);
}
$indices = $table->getIndices();
foreach ($indices as $index) {
$copiedIndex = clone $index;
$snapshotTable->addIndex($copiedIndex);
}
// copy unique indices to indices
// see https://github.com/propelorm/Propel/issues/175 for details
$unices = $table->getUnices();
foreach ($unices as $unique) {
$index = new Index();
$index->setName($unique->getName());
$columns = $unique->getColumns();
foreach ($columns as $columnName) {
if ($size = $unique->getColumnSize($columnName)) {
$index->addColumn(['name' => $columnName, 'size' => $size]);
} else {
$index->addColumn(['name' => $columnName]);
}
}
$snapshotTable->addIndex($index);
}
$behaviors = $database->getBehaviors();
foreach ($behaviors as $behavior) {
$behavior->modifyDatabase();
}
$this->snapshotTable = $snapshotTable;
}
示例12: addForeignKey
/**
* Adds a relation from logTable to origin table.
*
* @param Table $logTable
*/
protected function addForeignKey(Table $logTable)
{
$table = $this->getTable();
if ($table->getForeignKeysReferencingTable($table->getName())) {
//if already a foreignKey exist to origin table then don't add a second.
return;
}
// create the foreign key
$fk = new ForeignKey();
$fk->setForeignTableCommonName($table->getCommonName());
$fk->setForeignSchemaName($table->getSchema());
$fk->setPhpName('Origin');
$fk->setOnDelete('CASCADE');
$fk->setOnUpdate('CASCADE');
foreach ($table->getPrimaryKey() as $column) {
$fk->addReference($logTable->getColumn($column->getName()), $column);
}
$logTable->addForeignKey($fk);
}
示例13: addForeignKeys
/**
* Load foreign keys for this table.
*/
protected function addForeignKeys(Table $table)
{
$database = $table->getDatabase();
$dataFetcher = $this->dbh->query(sprintf('SHOW CREATE TABLE %s', $this->getPlatform()->doQuoting($table->getName())));
$row = $dataFetcher->fetch();
$foreignKeys = array();
// local store to avoid duplicates
// Get the information on all the foreign keys
$pattern = '/CONSTRAINT `([^`]+)` FOREIGN KEY \\((.+)\\) REFERENCES `([^\\s]+)` \\((.+)\\)(.*)/';
if (preg_match_all($pattern, $row[1], $matches)) {
$tmpArray = array_keys($matches[0]);
foreach ($tmpArray as $curKey) {
$name = $matches[1][$curKey];
$rawlcol = $matches[2][$curKey];
$ftbl = str_replace('`', '', $matches[3][$curKey]);
$rawfcol = $matches[4][$curKey];
$fkey = $matches[5][$curKey];
$lcols = array();
foreach (preg_split('/`, `/', $rawlcol) as $piece) {
$lcols[] = trim($piece, '` ');
}
$fcols = array();
foreach (preg_split('/`, `/', $rawfcol) as $piece) {
$fcols[] = trim($piece, '` ');
}
// typical for mysql is RESTRICT
$fkactions = array('ON DELETE' => ForeignKey::RESTRICT, 'ON UPDATE' => ForeignKey::RESTRICT);
if ($fkey) {
// split foreign key information -> search for ON DELETE and afterwords for ON UPDATE action
foreach (array_keys($fkactions) as $fkaction) {
$result = null;
preg_match('/' . $fkaction . ' (' . ForeignKey::CASCADE . '|' . ForeignKey::SETNULL . ')/', $fkey, $result);
if ($result && is_array($result) && isset($result[1])) {
$fkactions[$fkaction] = $result[1];
}
}
}
// restrict is the default
foreach ($fkactions as $key => $action) {
if (ForeignKey::RESTRICT === $action) {
$fkactions[$key] = null;
}
}
$localColumns = array();
$foreignColumns = array();
if ($table->guessSchemaName() != $database->getSchema() && false == strpos($ftbl, $database->getPlatform()->getSchemaDelimiter())) {
$ftbl = $table->guessSchemaName() . $database->getPlatform()->getSchemaDelimiter() . $ftbl;
}
$foreignTable = $database->getTable($ftbl, true);
if (!$foreignTable) {
continue;
}
foreach ($fcols as $fcol) {
$foreignColumns[] = $foreignTable->getColumn($fcol);
}
foreach ($lcols as $lcol) {
$localColumns[] = $table->getColumn($lcol);
}
if (!isset($foreignKeys[$name])) {
$fk = new ForeignKey($name);
$fk->setForeignTableCommonName($foreignTable->getCommonName());
if ($table->guessSchemaName() != $foreignTable->guessSchemaName()) {
$fk->setForeignSchemaName($foreignTable->guessSchemaName());
}
$fk->setOnDelete($fkactions['ON DELETE']);
$fk->setOnUpdate($fkactions['ON UPDATE']);
$table->addForeignKey($fk);
$foreignKeys[$name] = $fk;
}
$max = count($localColumns);
for ($i = 0; $i < $max; $i++) {
$foreignKeys[$name]->addReference($localColumns[$i], $foreignColumns[$i]);
}
}
}
}
示例14: testGetOnActionBehaviors
public function testGetOnActionBehaviors()
{
$fk = new ForeignKey();
$fk->setOnUpdate('SETNULL');
$fk->setOnDelete('CASCADE');
$this->assertSame('SET NULL', $fk->getOnUpdate());
$this->assertTrue($fk->hasOnUpdate());
$this->assertSame('CASCADE', $fk->getOnDelete());
$this->assertTrue($fk->hasOnDelete());
}
示例15: addForeignKeys
protected function addForeignKeys(Table $table)
{
$stmt = $this->dbh->query('PRAGMA foreign_key_list("' . $table->getName() . '")');
$lastId = null;
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
if ($lastId !== $row['id']) {
$fk = new ForeignKey();
$tableName = $row['table'];
$tableSchema = '';
if (false !== ($pos = strpos($tableName, '§'))) {
$tableName = substr($tableName, $pos + 2);
$tableSchema = substr($tableName, 0, $pos);
}
$fk->setForeignTableCommonName($tableName);
if ($table->getDatabase()->getSchema() != $tableSchema) {
$fk->setForeignSchemaName($tableSchema);
}
$fk->setOnDelete($row['on_delete']);
$fk->setOnUpdate($row['on_update']);
$table->addForeignKey($fk);
$lastId = $row['id'];
}
$fk->addReference($row['from'], $row['to']);
}
}