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);
     }
 }
 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']);
     }
 }
 public function testGetColumnForeignKeys()
 {
     $fk1 = $this->getForeignKeyMock('fk1', array('local_columns' => array('foo', 'author_id', 'bar')));
     $fk2 = $this->getForeignKeyMock('fk2', array('local_columns' => array('foo', 'bar')));
     $table = new Table();
     $table->addForeignKey($fk1);
     $table->addForeignKey($fk2);
     $this->assertCount(1, $table->getColumnForeignKeys('author_id'));
     $this->assertContains($fk1, $table->getColumnForeignKeys('author_id'));
 }
 /**
  * Load foreign keys for this table.
  */
 protected function addForeignKeys(Table $table)
 {
     $database = $table->getDatabase();
     $dataFetcher = $this->dbh->query("SELECT ccu1.TABLE_NAME, ccu1.COLUMN_NAME, ccu2.TABLE_NAME AS FK_TABLE_NAME, ccu2.COLUMN_NAME AS FK_COLUMN_NAME\n            FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu1 INNER JOIN\n            INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc1 ON tc1.CONSTRAINT_NAME = ccu1.CONSTRAINT_NAME AND\n            CONSTRAINT_TYPE = 'Foreign Key' INNER JOIN\n            INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1 ON rc1.CONSTRAINT_NAME = tc1.CONSTRAINT_NAME INNER JOIN\n            INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu2 ON ccu2.CONSTRAINT_NAME = rc1.UNIQUE_CONSTRAINT_NAME\n            WHERE (ccu1.table_name = '" . $table->getName() . "')");
     $foreignKeys = array();
     // local store to avoid duplicates
     foreach ($dataFetcher as $row) {
         $lcol = $this->cleanDelimitedIdentifiers($row['COLUMN_NAME']);
         $ftbl = $this->cleanDelimitedIdentifiers($row['FK_TABLE_NAME']);
         $fcol = $this->cleanDelimitedIdentifiers($row['FK_COLUMN_NAME']);
         $foreignTable = $database->getTable($ftbl);
         $foreignColumn = $foreignTable->getColumn($fcol);
         $localColumn = $table->getColumn($lcol);
         if (!isset($foreignKeys[$name])) {
             $fk = new ForeignKey($name);
             $fk->setForeignTableCommonName($foreignTable->getCommonName());
             $fk->setForeignSchemaName($foreignTable->getSchema());
             //$fk->setOnDelete($fkactions['ON DELETE']);
             //$fk->setOnUpdate($fkactions['ON UPDATE']);
             $table->addForeignKey($fk);
             $foreignKeys[$name] = $fk;
         }
         $foreignKeys[$name]->addReference($localColumn, $foreignColumn);
     }
 }
 /**
  * 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;
         }
     }
 }
 /**
  * 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]));
         }
     }
 }
 public function testCompareModifiedFks()
 {
     $db1 = new Database();
     $db1->setPlatform($this->platform);
     $c1 = new Column('Foo');
     $c2 = new Column('Bar');
     $fk1 = new ForeignKey();
     $fk1->addReference($c1, $c2);
     $t1 = new Table('Baz');
     $t1->addForeignKey($fk1);
     $db1->addTable($t1);
     $t1->doNaming();
     $db2 = new Database();
     $db2->setPlatform($this->platform);
     $c3 = new Column('Foo');
     $c4 = new Column('Bar2');
     $fk2 = new ForeignKey();
     $fk2->addReference($c3, $c4);
     $t2 = new Table('Baz');
     $t2->addForeignKey($fk2);
     $db2->addTable($t2);
     $t2->doNaming();
     $tc = new TableComparator();
     $tc->setFromTable($t1);
     $tc->setToTable($t2);
     $nbDiffs = $tc->compareForeignKeys();
     $tableDiff = $tc->getTableDiff();
     $this->assertEquals(1, $nbDiffs);
     $this->assertEquals(1, count($tableDiff->getModifiedFks()));
     $this->assertEquals(array('Baz_FK_1' => array($fk1, $fk2)), $tableDiff->getModifiedFks());
 }
 public function testCompareSort()
 {
     $c1 = new Column('Foo');
     $c2 = new Column('Bar');
     $c3 = new Column('Baz');
     $c4 = new Column('Faz');
     $fk1 = new ForeignKey();
     $fk1->addReference($c1, $c3);
     $fk1->addReference($c2, $c4);
     $t1 = new Table('Baz');
     $t1->addForeignKey($fk1);
     $fk2 = new ForeignKey();
     $fk2->addReference($c2, $c4);
     $fk2->addReference($c1, $c3);
     $t2 = new Table('Baz');
     $t2->addForeignKey($fk2);
     $this->assertFalse(PropelForeignKeyComparator::computeDiff($fk1, $fk2));
 }
 public function providerForTestGetForeignKeysDDL()
 {
     $table1 = new Table('foo');
     $column1 = new Column('bar_id');
     $column1->getDomain()->copy(new Domain('FOOTYPE'));
     $table1->addColumn($column1);
     $table2 = new Table('bar');
     $column2 = new Column('id');
     $column2->getDomain()->copy(new Domain('BARTYPE'));
     $table2->addColumn($column2);
     $fk = new ForeignKey('foo_bar_fk');
     $fk->setForeignTableCommonName('bar');
     $fk->addReference($column1, $column2);
     $fk->setOnDelete('CASCADE');
     $table1->addForeignKey($fk);
     $column3 = new Column('baz_id');
     $column3->getDomain()->copy(new Domain('BAZTYPE'));
     $table1->addColumn($column3);
     $table3 = new Table('baz');
     $column4 = new Column('id');
     $column4->getDomain()->copy(new Domain('BAZTYPE'));
     $table3->addColumn($column4);
     $fk = new ForeignKey('foo_baz_fk');
     $fk->setForeignTableCommonName('baz');
     $fk->addReference($column3, $column4);
     $fk->setOnDelete('SETNULL');
     $table1->addForeignKey($fk);
     return array(array($table1));
 }
 /**
  * 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);
 }
 /**
  * Load foreign keys for this table.
  */
 protected function addForeignKeys(Table $table)
 {
     $database = $table->getDatabase();
     $stmt = $this->dbh->query("SHOW CREATE TABLE `" . $table->getName() . "`");
     $row = $stmt->fetch(PDO::FETCH_NUM);
     $foreignKeys = array();
     // local store to avoid duplicates
     // Get the information on all the foreign keys
     $regEx = '/CONSTRAINT `([^`]+)` FOREIGN KEY \\((.+)\\) REFERENCES `([^`]*)` \\((.+)\\)(.*)/';
     if (preg_match_all($regEx, $row[1], $matches)) {
         $tmpArray = array_keys($matches[0]);
         foreach ($tmpArray as $curKey) {
             $name = $matches[1][$curKey];
             $rawlcol = $matches[2][$curKey];
             $ftbl = $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 ($action == ForeignKey::RESTRICT) {
                     $fkactions[$key] = null;
                 }
             }
             $localColumns = array();
             $foreignColumns = array();
             $foreignTable = $database->getTable($ftbl, true);
             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());
                 $fk->setForeignSchemaName($foreignTable->getSchema());
                 $fk->setOnDelete($fkactions['ON DELETE']);
                 $fk->setOnUpdate($fkactions['ON UPDATE']);
                 $table->addForeignKey($fk);
                 $foreignKeys[$name] = $fk;
             }
             for ($i = 0; $i < count($localColumns); $i++) {
                 $foreignKeys[$name]->addReference($localColumns[$i], $foreignColumns[$i]);
             }
         }
     }
 }
 /**
  * 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]);
             }
         }
     }
 }
Example #13
0
 /**
  * Load foreign keys for this table.
  */
 protected function addForeignKeys(Table $table)
 {
     $database = $table->getDatabase();
     $dataFetcher = $this->dbh->query("select fk.name as CONSTRAINT_NAME, lcol.name as COLUMN_NAME, rtab.name as FK_TABLE_NAME, rcol.name as FK_COLUMN_NAME\n         from sys.foreign_keys as fk \n         inner join sys.foreign_key_columns ref on ref.constraint_object_id = fk.object_id\n         inner join sys.columns lcol on lcol.object_id = ref.parent_object_id and lcol.column_id = ref.parent_column_id\n         inner join sys.columns rcol on rcol.object_id = ref.referenced_object_id and rcol.column_id = ref.referenced_column_id\n         inner join sys.tables rtab on rtab.object_id = ref.referenced_object_id\n         where fk.parent_object_id = OBJECT_ID('" . $table->getName() . "')");
     $dataFetcher->setStyle(\PDO::FETCH_ASSOC);
     $foreignKeys = [];
     // local store to avoid duplicates
     foreach ($dataFetcher as $row) {
         $name = $this->cleanDelimitedIdentifiers($row['CONSTRAINT_NAME']);
         $lcol = $this->cleanDelimitedIdentifiers($row['COLUMN_NAME']);
         $ftbl = $this->cleanDelimitedIdentifiers($row['FK_TABLE_NAME']);
         $fcol = $this->cleanDelimitedIdentifiers($row['FK_COLUMN_NAME']);
         $foreignTable = $database->getTable($ftbl);
         $foreignColumn = $foreignTable->getColumn($fcol);
         $localColumn = $table->getColumn($lcol);
         if (!isset($foreignKeys[$name])) {
             $fk = new ForeignKey($name);
             $fk->setForeignTableCommonName($foreignTable->getCommonName());
             $fk->setForeignSchemaName($foreignTable->getSchema());
             //$fk->setOnDelete($fkactions['ON DELETE']);
             //$fk->setOnUpdate($fkactions['ON UPDATE']);
             $table->addForeignKey($fk);
             $foreignKeys[$name] = $fk;
         }
         $foreignKeys[$name]->addReference($localColumn, $foreignColumn);
     }
 }
 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']);
     }
 }
Example #15
0
 public function startElement($parser, $name, $attributes)
 {
     $parentTag = $this->peekCurrentSchemaTag();
     if (false === $parentTag) {
         switch ($name) {
             case 'database':
                 if ($this->isExternalSchema()) {
                     $this->currentPackage = isset($attributes['package']) ? $attributes['package'] : null;
                     if (null === $this->currentPackage) {
                         $this->currentPackage = $this->defaultPackage;
                     }
                 } else {
                     $this->currDB = $this->schema->addDatabase($attributes);
                 }
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('database' === $parentTag) {
         switch ($name) {
             case 'external-schema':
                 $xmlFile = isset($attributes['filename']) ? $attributes['filename'] : null;
                 // 'referenceOnly' attribute is valid in the main schema XML file only,
                 // and it's ignored in the nested external-schemas
                 if (!$this->isExternalSchema()) {
                     $isForRefOnly = isset($attributes['referenceOnly']) ? $attributes['referenceOnly'] : null;
                     $this->isForReferenceOnly = null !== $isForRefOnly ? 'true' === strtolower($isForRefOnly) : true;
                     // defaults to TRUE
                 }
                 if ('/' !== $xmlFile[0]) {
                     $xmlFile = realpath(dirname($this->currentXmlFile) . DIRECTORY_SEPARATOR . $xmlFile);
                     if (!file_exists($xmlFile)) {
                         throw new SchemaException(sprintf('Unknown include external "%s"', $xmlFile));
                     }
                 }
                 $this->parseFile($xmlFile);
                 break;
             case 'domain':
                 $this->currDB->addDomain($attributes);
                 break;
             case 'table':
                 if (!isset($attributes['schema']) && $this->currDB->getSchema() && $this->currDB->getPlatform()->supportsSchemas() && false === strpos($attributes['name'], $this->currDB->getPlatform()->getSchemaDelimiter())) {
                     $attributes['schema'] = $this->currDB->getSchema();
                 }
                 $this->currTable = $this->currDB->addTable($attributes);
                 if ($this->isExternalSchema()) {
                     $this->currTable->setForReferenceOnly($this->isForReferenceOnly);
                     $this->currTable->setPackage($this->currentPackage);
                 }
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currDB->addVendorInfo($attributes);
                 break;
             case 'behavior':
                 $this->currBehavior = $this->currDB->addBehavior($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('table' === $parentTag) {
         switch ($name) {
             case 'column':
                 $this->currColumn = $this->currTable->addColumn($attributes);
                 break;
             case 'foreign-key':
                 $this->currFK = $this->currTable->addForeignKey($attributes);
                 break;
             case 'index':
                 $this->currIndex = new Index();
                 $this->currIndex->setTable($this->currTable);
                 $this->currIndex->loadMapping($attributes);
                 break;
             case 'unique':
                 $this->currUnique = new Unique();
                 $this->currUnique->setTable($this->currTable);
                 $this->currUnique->loadMapping($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currTable->addVendorInfo($attributes);
                 break;
             case 'id-method-parameter':
                 $this->currTable->addIdMethodParameter($attributes);
                 break;
             case 'behavior':
                 $this->currBehavior = $this->currTable->addBehavior($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('column' === $parentTag) {
         switch ($name) {
             case 'inheritance':
                 $this->currColumn->addInheritance($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currColumn->addVendorInfo($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('foreign-key' === $parentTag) {
         switch ($name) {
             case 'reference':
                 $this->currFK->addReference($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currUnique->addVendorInfo($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('index' === $parentTag) {
         switch ($name) {
             case 'index-column':
                 $this->currIndex->addColumn($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currIndex->addVendorInfo($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('unique' === $parentTag) {
         switch ($name) {
             case 'unique-column':
                 $this->currUnique->addColumn($attributes);
                 break;
             case 'vendor':
                 $this->currVendorObject = $this->currUnique->addVendorInfo($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ($parentTag == 'behavior') {
         switch ($name) {
             case 'parameter':
                 $this->currBehavior->addParameter($attributes);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } elseif ('vendor' === $parentTag) {
         switch ($name) {
             case 'parameter':
                 $this->currVendorObject->setParameter($attributes['name'], $attributes['value']);
                 break;
             default:
                 $this->_throwInvalidTagException($parser, $name);
         }
     } else {
         // it must be an invalid tag
         $this->_throwInvalidTagException($parser, $name);
     }
     $this->pushCurrentSchemaTag($name);
 }