Ejemplo n.º 1
0
 public function modifyTable()
 {
     $parentTable = $this->getParentTable();
     if (count($parentTable->getPrimaryKey()) > 1) {
         throw new RuntimeException('Equal nest works only with a single primary key for the parent table');
     }
     $parentTablePrimaryKey = $parentTable->getPrimaryKey();
     if (!$this->getTable()->hasColumn($this->getReferenceColumn1Name())) {
         $this->getTable()->addColumn(array('name' => $this->getReferenceColumn1Name(), 'primaryKey' => 'true', 'type' => $parentTablePrimaryKey[0]->getType()));
         $fk = new ForeignKey();
         $fk->setName($this->getReferenceColumn1Name());
         $fk->setForeignTableCommonName($this->getParentTable()->getCommonName());
         $fk->setOnDelete(ForeignKey::CASCADE);
         $fk->setOnUpdate(null);
         $fk->addReference($this->getReferenceColumn1Name(), $parentTablePrimaryKey[0]->getName());
         $this->getTable()->addForeignKey($fk);
     }
     if (!$this->getTable()->hasColumn($this->getReferenceColumn2Name())) {
         $this->getTable()->addColumn(array('name' => $this->getReferenceColumn2Name(), 'primaryKey' => 'true', 'type' => $parentTablePrimaryKey[0]->getType()));
         $fk = new ForeignKey();
         $fk->setName($this->getReferenceColumn2Name());
         $fk->setForeignTableCommonName($this->getParentTable()->getCommonName());
         $fk->setOnDelete(ForeignKey::CASCADE);
         $fk->setOnUpdate(null);
         $fk->addReference($this->getReferenceColumn2Name(), $parentTablePrimaryKey[0]->getName());
         $this->getTable()->addForeignKey($fk);
     }
     if (!$parentTable->hasBehavior('equal_nest_parent')) {
         $parentBehavior = new EqualNestParentBehavior();
         $parentBehavior->setName('equal_nest_parent');
         $parentBehavior->addParameter(array('name' => 'middle_table', 'value' => $this->getTable()->getName()));
         $parentTable->addBehavior($parentBehavior);
     }
     $this->parentBehavior = $parentTable->getBehavior('equal_nest_parent');
 }
Ejemplo n.º 2
0
 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);
 }
Ejemplo n.º 3
0
 private function addRelation($oLocalColumn, $oUsersTable, $oUsersId)
 {
     $oFk = new ForeignKey();
     $oFk->setTable($this->getTable());
     $oFk->setForeignTableCommonName($oUsersTable->getName());
     if (!$oFk->isMatchedByInverseFK()) {
         $oFk->setOnDelete(ForeignKey::SETNULL);
         $oFk->setOnUpdate(null);
         $oFk->addReference($oLocalColumn, $oUsersId);
         $this->getTable()->addForeignKey($oFk);
     }
 }
Ejemplo n.º 4
0
 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));
         // 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;
             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);
     }
 }
Ejemplo n.º 5
0
 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);
 }
 /**
  * Load foreign keys for this table.
  */
 protected function addForeignKeys(Table $table, $oid, $version)
 {
     $database = $table->getDatabase();
     $stmt = $this->dbh->prepare("SELECT\n\t\t\t\t\t\t\t\t          conname,\n\t\t\t\t\t\t\t\t          confupdtype,\n\t\t\t\t\t\t\t\t          confdeltype,\n\t\t\t\t\t\t\t\t          CASE nl.nspname WHEN 'public' THEN cl.relname ELSE nl.nspname||'.'||cl.relname END as fktab,\n\t\t\t\t\t\t\t\t          a2.attname as fkcol,\n\t\t\t\t\t\t\t\t          CASE nr.nspname WHEN 'public' THEN cr.relname ELSE nr.nspname||'.'||cr.relname END as reftab,\n\t\t\t\t\t\t\t\t          a1.attname as refcol\n\t\t\t\t\t\t\t\t    FROM pg_constraint ct\n\t\t\t\t\t\t\t\t         JOIN pg_class cl ON cl.oid=conrelid\n\t\t\t\t\t\t\t\t         JOIN pg_class cr ON cr.oid=confrelid\n\t\t\t\t\t\t\t\t         JOIN pg_namespace nl ON nl.oid = cl.relnamespace\n\t\t\t\t\t\t\t\t         JOIN pg_namespace nr ON nr.oid = cr.relnamespace\n\t\t\t\t\t\t\t\t         LEFT JOIN pg_catalog.pg_attribute a1 ON a1.attrelid = ct.confrelid\n\t\t\t\t\t\t\t\t         LEFT JOIN pg_catalog.pg_attribute a2 ON a2.attrelid = ct.conrelid\n\t\t\t\t\t\t\t\t    WHERE\n\t\t\t\t\t\t\t\t         contype='f'\n\t\t\t\t\t\t\t\t         AND conrelid = ?\n\t\t\t\t\t\t\t\t         AND a2.attnum = ct.conkey[1]\n\t\t\t\t\t\t\t\t         AND a1.attnum = ct.confkey[1]\n\t\t\t\t\t\t\t\t    ORDER BY conname");
     $stmt->bindValue(1, $oid);
     $stmt->execute();
     $foreignKeys = array();
     // local store to avoid duplicates
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $name = $row['conname'];
         $local_table = $row['fktab'];
         $local_column = $row['fkcol'];
         $foreign_table = $row['reftab'];
         $foreign_column = $row['refcol'];
         // 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($foreign_table);
         $foreignColumn = $foreignTable->getColumn($foreign_column);
         $localTable = $database->getTable($local_table);
         $localColumn = $localTable->getColumn($local_column);
         if (!isset($foreignKeys[$name])) {
             $fk = new ForeignKey($name);
             $fk->setForeignTableName($foreignTable->getName());
             $fk->setOnDelete($ondelete);
             $fk->setOnUpdate($onupdate);
             $table->addForeignKey($fk);
             $foreignKeys[$name] = $fk;
         }
         $foreignKeys[$name]->addReference($localColumn, $foreignColumn);
     }
 }
Ejemplo n.º 7
0
 /**
  * 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];
                     }
                 }
             }
             $localColumns = array();
             $foreignColumns = array();
             $foreignTable = $database->getTable($ftbl);
             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->setForeignTableName($foreignTable->getName());
                 $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]);
             }
         }
     }
 }
Ejemplo n.º 8
0
 /**
  * Load foreign keys for this table.
  */
 protected function addForeignKeys(Table $table, $oid, $version)
 {
     $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();
     // local store to avoid duplicates
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $name = $row['conname'];
         $local_table = $row['fktab'];
         $local_columns = explode(',', trim($row['fkcols'], '{}'));
         $foreign_table = $row['reftab'];
         $foreign_columns = 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($foreign_table);
         $localTable = $database->getTable($local_table);
         if (!isset($foreignKeys[$name])) {
             $fk = new ForeignKey($name);
             $fk->setForeignTableCommonName($foreignTable->getCommonName());
             $fk->setForeignSchemaName($foreignTable->getSchema());
             $fk->setOnDelete($ondelete);
             $fk->setOnUpdate($onupdate);
             $table->addForeignKey($fk);
             $foreignKeys[$name] = $fk;
         }
         for ($i = 0; $i < count($local_columns); $i++) {
             $foreignKeys[$name]->addReference($localTable->getColumn($local_columns[$i]), $foreignTable->getColumn($foreign_columns[$i]));
         }
     }
 }
Ejemplo n.º 9
0
 /**
  * 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();
     $stmt = $this->dbh->query("SELECT CONSTRAINT_NAME, DELETE_RULE, R_CONSTRAINT_NAME FROM USER_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'R' AND TABLE_NAME = '" . $table->getName() . "'");
     /* @var stmt PDOStatement */
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         // Local reference
         $stmt2 = $this->dbh->query("SELECT COLUMN_NAME FROM USER_CONS_COLUMNS WHERE CONSTRAINT_NAME = '" . $row['CONSTRAINT_NAME'] . "' AND TABLE_NAME = '" . $table->getName() . "'");
         /* @var stmt2 PDOStatement */
         $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 = $row["DELETE_RULE"] == 'NO ACTION' ? '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;
         }
     }
 }
 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->containsColumn($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 validators of the parent table
     foreach ($parentTable->getValidators() as $validator) {
         $copiedValidator = clone $validator;
         $this->getTable()->addValidator($copiedValidator);
     }
     // 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;
         }
         $copiedBehavior = clone $behavior;
         $copiedBehavior->setTableModified(false);
         $this->getTable()->addBehavior($copiedBehavior);
     }
 }
Ejemplo n.º 11
0
 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));
 }
 protected function createTaggingTable()
 {
     $table = $this->getTable();
     $database = $table->getDatabase();
     $pks = $this->getTable()->getPrimaryKey();
     if (count($pks) > 1) {
         throw new EngineException('The Taggable behavior does not support tables with composite primary keys');
     }
     $taggingTableName = $this->getTaggingTableName();
     if ($database->hasTable($taggingTableName)) {
         $this->taggingTable = $database->getTable($taggingTableName);
     } else {
         $this->taggingTable = $database->addTable(array('name' => $taggingTableName, 'phpName' => $this->replaceTokens($this->getParameter('tagging_table_phpname')), 'package' => $table->getPackage(), 'schema' => $table->getSchema(), 'namespace' => '\\' . $table->getNamespace()));
         // every behavior adding a table should re-execute database behaviors
         // see bug 2188 http://www.propelorm.org/changeset/2188
         foreach ($database->getBehaviors() as $behavior) {
             $behavior->modifyDatabase();
         }
     }
     if ($this->taggingTable->hasColumn('tag_id')) {
         $tagFkColumn = $this->taggingTable->getColumn('tag_id');
     } else {
         $tagFkColumn = $this->taggingTable->addColumn(array('name' => 'tag_id', 'type' => PropelTypes::INTEGER, 'primaryKey' => 'true'));
     }
     if ($this->taggingTable->hasColumn($table->getName() . '_id')) {
         $objFkColumn = $this->taggingTable->getColumn($table->getName() . '_id');
     } else {
         $objFkColumn = $this->taggingTable->addColumn(array('name' => $table->getName() . '_id', 'type' => PropelTypes::INTEGER, 'primaryKey' => 'true'));
     }
     $this->taggingTable->setIsCrossRef(true);
     $fkTag = new ForeignKey();
     $fkTag->setForeignTableCommonName($this->tagTable->getCommonName());
     $fkTag->setForeignSchemaName($this->tagTable->getSchema());
     $fkTag->setOnDelete(ForeignKey::CASCADE);
     $fkTag->setOnUpdate(ForeignKey::CASCADE);
     $tagColumn = $this->tagTable->getColumn('id');
     $fkTag->addReference($tagFkColumn->getName(), $tagColumn->getName());
     $this->taggingTable->addForeignKey($fkTag);
     $fkObj = new ForeignKey();
     $fkObj->setForeignTableCommonName($this->getTable()->getCommonName());
     $fkObj->setForeignSchemaName($this->getTable()->getSchema());
     $fkObj->setOnDelete(ForeignKey::CASCADE);
     $fkObj->setOnUpdate(ForeignKey::CASCADE);
     foreach ($pks as $column) {
         $fkObj->addReference($objFkColumn->getName(), $column->getName());
     }
     $this->taggingTable->addForeignKey($fkObj);
 }
 public function testCompareOnDelete()
 {
     $c1 = new Column('Foo');
     $c2 = new Column('Bar');
     $fk1 = new ForeignKey();
     $fk1->addReference($c1, $c2);
     $fk1->setOnDelete(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->setOnDelete(ForeignKey::RESTRICT);
     $t2 = new Table('Baz');
     $t2->addForeignKey($fk2);
     $this->assertTrue(PropelForeignKeyComparator::computeDiff($fk1, $fk2));
 }
Ejemplo n.º 14
0
 /**
  * Load foreign keys for this table.
  */
 protected function addForeignKeys(Table $table)
 {
     $database = $table->getDatabase();
     $stmt = $this->dbh->query("SELECT CONSTRAINT_NAME, DELETE_RULE, R_CONSTRAINT_NAME FROM ALL_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'R' AND TABLE_NAME = '" . $table->getName() . "'");
     $foreignKeys = array();
     // local store to avoid duplicates
     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $name = $row['CONSTRAINT_NAME'];
         $stmt2 = $this->dbh->query("SELECT CON.CONSTRAINT_NAME, CON.TABLE_NAME, COL.COLUMN_NAME FROM USER_CONS_COLUMNS COL, USER_CONSTRAINTS CON WHERE COL.CONSTRAINT_NAME = CON.CONSTRAINT_NAME AND COL.CONSTRAINT_NAME = '" . $row['R_CONSTRAINT_NAME'] . "'");
         $foreignKeyCols = $stmt2->fetch(PDO::FETCH_ASSOC);
         if (!is_array($foreignKeyCols[0])) {
             $foreignRows[0] = $foreignKeyCols;
         } else {
             $foreignRows = $foreignKeyCols;
         }
         $stmt2 = $this->dbh->query("SELECT CON.CONSTRAINT_NAME, CON.TABLE_NAME, COL.COLUMN_NAME FROM USER_CONS_COLUMNS COL, USER_CONSTRAINTS CON WHERE COL.CONSTRAINT_NAME = CON.CONSTRAINT_NAME AND COL.CONSTRAINT_NAME = '" . $row['CONSTRAINT_NAME'] . "'");
         $localKeyCols = $stmt2->fetch(PDO::FETCH_ASSOC);
         if (!is_array($localKeyCols[0])) {
             $localRows[0] = $localKeyCols;
         } else {
             $localRows = $localKeyCols;
         }
         print_r($foreignRows);
         $localColumns = array();
         $foreignColumns = array();
         $foreignTable = $database->getTable($foreignRows[0]["TABLE_NAME"]);
         foreach ($foreignRows as $foreignCol) {
             $foreignColumns[] = $foreignTable->getColumn($foreignCol["COLUMN_NAME"]);
         }
         foreach ($localRows as $localCol) {
             $localColumns[] = $table->getColumn($localCol['COLUMN_NAME']);
         }
         if (!isset($foreignKeys[$name])) {
             $fk = new ForeignKey($name);
             $fk->setForeignTableName($foreignTable->getName());
             $fk->setOnDelete($row["DELETE_RULE"]);
             $fk->setOnUpdate($row["DELETE_RULE"]);
             $table->addForeignKey($fk);
             $foreignKeys[$name] = $fk;
         }
         for ($i = 0; $i < count($localColumns); $i++) {
             $foreignKeys[$name]->addReference($localColumns[$i], $foreignColumns[$i]);
         }
     }
 }