public function getDropForeignKeyDDL(ForeignKey $fk) { if (!$this->supportsForeignKeys($fk->getTable())) { return ''; } if ($fk->isSkipSql()) { return; } $pattern = "\nALTER TABLE %s DROP FOREIGN KEY %s;\n"; return sprintf($pattern, $this->quoteIdentifier($fk->getTable()->getName()), $this->quoteIdentifier($fk->getName())); }
public function getAddForeignKeyDDL(ForeignKey $fk) { if ($fk->isSkipSql() || $fk->isPolymorphic()) { return; } $pattern = "\nBEGIN\nALTER TABLE %s ADD %s\nEND\n;\n"; return sprintf($pattern, $this->quoteIdentifier($fk->getTable()->getName()), $this->getForeignKeyDDL($fk)); }
protected function addRefFkScheduledForDeletionAttribute(&$script, ForeignKey $refFK) { $className = $this->getClassNameFromTable($refFK->getTable()); $fkName = lcfirst($this->getRefFKPhpNameAffix($refFK, true)); $script .= "\n /**\n * An array of objects scheduled for deletion.\n * @var ObjectCollection|{$className}[]\n */\n protected \${$fkName}ScheduledForDeletion = null;\n"; }
protected static function getRefRelatedBySuffix(ForeignKey $fk) { $relCol = ''; foreach ($fk->getLocalForeignMapping() as $localColumnName => $foreignColumnName) { $localTable = $fk->getTable(); $localColumn = $localTable->getColumn($localColumnName); if (!$localColumn) { throw new RuntimeException(sprintf('Could not fetch column: %s in table %s.', $localColumnName, $localTable->getName())); } $foreignKeysToForeignTable = $localTable->getForeignKeysReferencingTable($fk->getForeignTableName()); if ($fk->getForeignTableName() == $fk->getTableName()) { // self referential foreign key $relCol .= $fk->getForeignTable()->getColumn($foreignColumnName)->getPhpName(); if (count($foreignKeysToForeignTable) > 1) { // several self-referential foreign keys $relCol .= array_search($fk, $foreignKeysToForeignTable); } } elseif (count($foreignKeysToForeignTable) > 1 || count($fk->getForeignTable()->getForeignKeysReferencingTable($fk->getTableName())) > 0) { // several foreign keys to the same table, or symmetrical foreign key in foreign table $relCol .= $localColumn->getPhpName(); } } if (!empty($relCol)) { $relCol = 'RelatedBy' . $relCol; } return $relCol; }
/** * Builds the DDL SQL to drop a foreign key. * * @param ForeignKey $fk * @return string */ public function getDropForeignKeyDDL(ForeignKey $fk) { if ($fk->isSkipSql() || $fk->isPolymorphic()) { return; } $pattern = "\nALTER TABLE %s DROP CONSTRAINT %s;\n"; return sprintf($pattern, $this->quoteIdentifier($fk->getTable()->getName()), $this->quoteIdentifier($fk->getName())); }
public function getReferrerVersionsColumn(ForeignKey $fk) { $fkTableName = $fk->getTable()->getName(); $fkIdsColumnName = $fkTableName . '_versions'; return $this->versionTable->getColumn($fkIdsColumnName); }
/** * @param string &$script The script will be modified in this method. * @param ForeignKey $refFK * @param ForeignKey $crossFK */ protected function addCrossFKDoAdd(&$script, ForeignKey $refFK, ForeignKey $crossFK) { $relatedObjectClassName = $this->getFKPhpNameAffix($crossFK, $plural = false); // lcfirst() doesn't exist in PHP < 5.3 $lowerRelatedObjectClassName = $relatedObjectClassName; $lowerRelatedObjectClassName[0] = strtolower($lowerRelatedObjectClassName[0]); $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable()); $className = $joinedTableObjectBuilder->getObjectClassname(); $tblFK = $refFK->getTable(); $foreignObjectName = '$' . $tblFK->getStudlyPhpName(); $script .= "\n /**\n * @param {$relatedObjectClassName} \${$lowerRelatedObjectClassName} The {$lowerRelatedObjectClassName} object to add.\n */\n protected function doAdd{$relatedObjectClassName}(\${$lowerRelatedObjectClassName})\n {\n {$foreignObjectName} = new {$className}();\n {$foreignObjectName}->set{$relatedObjectClassName}(\${$lowerRelatedObjectClassName});\n \$this->add{$className}({$foreignObjectName});\n }\n"; }
public function testSetTable() { $table = $this->getTableMock('book'); $table->expects($this->once())->method('getSchema')->will($this->returnValue('books')); $fk = new ForeignKey(); $fk->setTable($table); $this->assertInstanceOf('Propel\\Generator\\Model\\Table', $fk->getTable()); $this->assertSame('books', $fk->getSchemaName()); $this->assertSame('book', $fk->getTableName()); }
/** * Adds the method that remove an object from the referrer fkey collection. * @param string $script The script will be modified in this method. * @param ForeignKey $refFK * @param ForeignKey $crossFK */ protected function addCrossFKRemove(&$script, ForeignKey $refFK, ForeignKey $crossFK) { $relCol = $this->getFKPhpNameAffix($crossFK, $plural = true); $collName = 'coll' . $relCol; $tblFK = $refFK->getTable(); $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable()); $className = $joinedTableObjectBuilder->getObjectClassname(); $M2MScheduledForDeletion = lcfirst($relCol) . "ScheduledForDeletion"; $crossObjectName = '$' . $crossFK->getForeignTable()->getStudlyPhpName(); $crossObjectClassName = $this->getNewObjectBuilder($crossFK->getForeignTable())->getObjectClassname(); $relatedObjectClassName = $this->getFKPhpNameAffix($crossFK, $plural = false); $script .= "\n /**\n * Remove a {$crossObjectClassName} object to this object\n * through the {$tblFK->getName()} cross reference table.\n *\n * @param {$crossObjectClassName} {$crossObjectName} The {$className} object to relate\n * @return " . $this->getObjectClassname() . " The current object (for fluent API support)\n */\n public function remove{$relatedObjectClassName}({$crossObjectClassName} {$crossObjectName})\n {\n if (\$this->get{$relCol}()->contains({$crossObjectName})) {\n \$this->{$collName}->remove(\$this->{$collName}->search({$crossObjectName}));\n\n if (null === \$this->{$M2MScheduledForDeletion}) {\n \$this->{$M2MScheduledForDeletion} = clone \$this->{$collName};\n \$this->{$M2MScheduledForDeletion}->clear();\n }\n\n \$this->{$M2MScheduledForDeletion}[] = {$crossObjectName};\n }\n\n return \$this;\n }\n"; }