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; }
protected function addCrossFkScheduledForDeletionAttribute(&$script, ForeignKey $crossFK) { $className = $this->getClassNameFromTable($crossFK->getForeignTable()); $fkName = lcfirst($this->getFKPhpNameAffix($crossFK, true)); $script .= "\n /**\n * An array of objects scheduled for deletion.\n * @var ObjectCollection|{$className}[]\n */\n protected \${$fkName}ScheduledForDeletion = null;\n"; }
protected function addCrossFKAttributes(&$script, ForeignKey $crossFK) { $joinedTableObjectBuilder = $this->getNewObjectBuilder($crossFK->getForeignTable()); $className = $joinedTableObjectBuilder->getObjectClassname(); $relatedName = $this->getFKPhpNameAffix($crossFK, $plural = true); $script .= "\n /**\n * @var array {$className}[] Collection to store aggregation of {$className} objects.\n */\n protected \$" . $this->getCrossFKVarName($crossFK) . ";\n"; }
/** * Gets the "RelatedBy*" suffix (if needed) that is attached to method and variable names. * * The related by suffix is based on the local columns of the foreign key. If there is more than * one column in a table that points to the same foreign table, then a 'RelatedByLocalColName' suffix * will be appended. * * @return string */ protected static function getRelatedBySuffix(ForeignKey $fk) { $relCol = ''; foreach ($fk->getMapping() as $mapping) { list($localColumn, $foreignValueOrColumn) = $mapping; $localColumnName = $localColumn->getPhpName(); $localTable = $fk->getTable(); if (!$localColumn) { throw new RuntimeException(sprintf('Could not fetch column: %s in table %s.', $localColumnName, $localTable->getName())); } if (count($localTable->getForeignKeysReferencingTable($fk->getForeignTableName())) > 1 || count($fk->getForeignTable()->getForeignKeysReferencingTable($fk->getTableName())) > 0 || $fk->getForeignTableName() == $fk->getTableName()) { // self referential foreign key, or several foreign keys to the same table, or cross-reference fkey $relCol .= $localColumn->getPhpName(); } } if (!empty($relCol)) { $relCol = 'RelatedBy' . $relCol; } return $relCol; }
public function testGetInverseForeignKey() { $database = $this->getDatabaseMock('bookstore'); $platform = $this->getPlatformMock(true); $foreignTable = $this->getTableMock('authors'); $localTable = $this->getTableMock('books', ['platform' => $platform, 'database' => $database]); $database->expects($this->any())->method('getTable')->with($this->equalTo('bookstore.authors'))->will($this->returnValue($foreignTable)); $inversedFk = new ForeignKey(); $inversedFk->addReference('id', 'author_id'); $inversedFk->setTable($localTable); $inversedFk->setForeignSchemaName('bookstore'); $inversedFk->setForeignTableCommonName('authors'); $foreignTable->expects($this->any())->method('getForeignKeys')->will($this->returnValue([$inversedFk])); $fk = new ForeignKey(); $fk->setTable($localTable); $fk->addReference('author_id', 'id'); $fk->setForeignSchemaName('bookstore'); $fk->setForeignTableCommonName('authors'); $this->assertSame('authors', $fk->getForeignTableCommonName()); $this->assertSame('bookstore.authors', $fk->getForeignTableName()); $this->assertInstanceOf('Propel\\Generator\\Model\\Table', $fk->getForeignTable()); $this->assertSame($inversedFk, $fk->getInverseFK()); $this->assertTrue($fk->isMatchedByInverseFK()); }