/** * Adds the method that returns the referrer fkey collection. * @param string &$script The script will be modified in this method. */ protected function addRefFKGet(&$script, ForeignKey $refFK) { $table = $this->getTable(); $tblFK = $refFK->getTable(); $peerClassname = $this->getStubPeerBuilder()->getClassname(); $fkPeerBuilder = $this->getNewPeerBuilder($refFK->getTable()); $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true); $collName = $this->getRefFKCollVarName($refFK); $lastCriteriaName = $this->getRefFKLastCriteriaVarName($refFK); $className = $fkPeerBuilder->getObjectClassname(); $script .= "\n\t/**\n\t * Gets an array of {$className} objects which contain a foreign key that references this object.\n\t *\n\t * If this collection has already been initialized with an identical Criteria, it returns the collection.\n\t * Otherwise if this " . $this->getObjectClassname() . " has previously been saved, it will retrieve\n\t * related {$relCol} from storage. If this " . $this->getObjectClassname() . " is new, it will return\n\t * an empty collection or the current collection, the criteria is ignored on a new object.\n\t *\n\t * @param PropelPDO \$con\n\t * @param Criteria \$criteria\n\t * @return array {$className}[]\n\t * @throws PropelException\n\t */\n\tpublic function get{$relCol}(\$criteria = null, PropelPDO \$con = null, array \$selectedColumns = null)\n\t{"; $script .= "\n\t\tif (\$criteria === null) {\n\t\t\t\$criteria = new Criteria({$peerClassname}::DATABASE_NAME);\n\t\t}\n\t\telseif (\$criteria instanceof Criteria)\n\t\t{\n\t\t\t\$criteria = clone \$criteria;\n\t\t}\n\n\t\tif (\$this->{$collName} === null) {\n\t\t\tif (\$this->isNew()) {\n\t\t\t \$this->{$collName} = array();\n\t\t\t} else {\n"; foreach ($refFK->getLocalColumns() as $colFKName) { // $colFKName is local to the referring table (i.e. foreign to this table) $lfmap = $refFK->getLocalForeignMapping(); $localColumn = $this->getTable()->getColumn($lfmap[$colFKName]); $colFK = $refFK->getTable()->getColumn($colFKName); $clo = strtolower($localColumn->getName()); $script .= "\n\t\t\t\t\$criteria->add(" . $fkPeerBuilder->getColumnConstant($colFK) . ", \$this->{$clo});\n"; } // end foreach ($fk->getForeignColumns() $script .= "\n if (null !== \$selectedColumns) {\n " . $fkPeerBuilder->getPeerClassname() . "::setSelectedColumns(\$selectedColumns);\n }\n\t\t\t\t" . $fkPeerBuilder->getPeerClassname() . "::addSelectColumns(\$criteria);\n\t\t\t\t\$this->{$collName} = " . $fkPeerBuilder->getPeerClassname() . "::doSelect(\$criteria, \$con);\n\t\t\t}\n\t\t} else {\n\t\t\t// criteria has no effect for a new object\n\t\t\tif (!\$this->isNew()) {\n\t\t\t\t// the following code is to determine if a new query is\n\t\t\t\t// called for. If the criteria is the same as the last\n\t\t\t\t// one, just return the collection.\n"; foreach ($refFK->getLocalColumns() as $colFKName) { // $colFKName is local to the referring table (i.e. foreign to this table) $lfmap = $refFK->getLocalForeignMapping(); $localColumn = $this->getTable()->getColumn($lfmap[$colFKName]); $colFK = $refFK->getTable()->getColumn($colFKName); $clo = strtolower($localColumn->getName()); $script .= "\n\n\t\t\t\t\$criteria->add(" . $fkPeerBuilder->getColumnConstant($colFK) . ", \$this->{$clo});\n"; } // foreach ($fk->getForeignColumns() $script .= "\n if (null !== \$selectedColumns) {\n " . $fkPeerBuilder->getPeerClassname() . "::setSelectedColumns(\$selectedColumns);\n }\n\t\t\t\t" . $fkPeerBuilder->getPeerClassname() . "::addSelectColumns(\$criteria);\n\t\t\t\tif (!isset(\$this->{$lastCriteriaName}) || !\$this->" . $lastCriteriaName . "->equals(\$criteria)) {\n\t\t\t\t\t\$this->{$collName} = " . $fkPeerBuilder->getPeerClassname() . "::doSelect(\$criteria, \$con);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\$this->{$lastCriteriaName} = \$criteria;\n\t\treturn \$this->{$collName};\n\t}\n"; }
public function getAddForeignKeyDDL(ForeignKey $fk) { if ($fk->isSkipSql()) { return; } $pattern = "\nBEGIN\nALTER TABLE %s ADD %s\nEND\n;\n"; return sprintf($pattern, $this->quoteIdentifier($fk->getTable()->getName()), $this->getForeignKeyDDL($fk)); }
/** * 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 function getRelatedBySuffix(ForeignKey $fk, $columnCheck = false) { $relCol = ""; foreach ($fk->getLocalColumns() as $columnName) { $column = $fk->getTable()->getColumn($columnName); if (!$column) { throw new Exception("Could not fetch column: {$columnName} in table " . $fk->getTable()->getName()); } if (count($column->getTable()->getForeignKeysReferencingTable($fk->getForeignTableName())) > 1 || $fk->getForeignTableName() == $fk->getTable()->getName()) { // if there are seeral foreign keys that point to the same table // then we need to generate methods like getAuthorRelatedByColName() // instead of just getAuthor(). Currently we are doing the same // for self-referential foreign keys, to avoid confusion. $relCol .= $column->getPhpName(); } } #var_dump($fk->getForeignTableName() . ' - ' .$fk->getTableName() . ' - ' . $this->getTable()->getName()); #$fk->getForeignTableName() != $this->getTable()->getName() && // @todo comment on it if ($columnCheck && !$relCol && $fk->getTable()->getColumn($fk->getForeignTableName())) { foreach ($fk->getLocalColumns() as $columnName) { $column = $fk->getTable()->getColumn($columnName); $relCol .= $column->getPhpName(); } } if ($relCol != "") { $relCol = "RelatedBy" . $relCol; } return $relCol; }
/** * Adds the method that remove an object from the referrer fkey collection. * @param string $script The script will be modified in this method. */ 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(); $lowerRelCol = $relCol; $lowerRelCol[0] = strtolower($lowerRelCol[0]); $M2MScheduledForDeletion = $lowerRelCol . "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 void\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 if (null === \$this->{$M2MScheduledForDeletion}) {\n \$this->{$M2MScheduledForDeletion} = clone \$this->{$collName};\n \$this->{$M2MScheduledForDeletion}->clear();\n }\n \$this->{$M2MScheduledForDeletion}[]= {$crossObjectName};\n }\n }\n"; }
/** * @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\t/**\n\t * @param\t{$relatedObjectClassName} \${$lowerRelatedObjectClassName} The {$lowerRelatedObjectClassName} object to add.\n\t */\n\tprotected function doAdd{$relatedObjectClassName}(\${$lowerRelatedObjectClassName})\n\t{\n\t\t{$foreignObjectName} = new {$className}();\n\t\t{$foreignObjectName}->set{$relatedObjectClassName}(\${$lowerRelatedObjectClassName});\n\t\t\$this->add{$className}({$foreignObjectName});\n\t}\n"; }
public function getReferrerVersionsColumn(ForeignKey $fk) { $fkTableName = $fk->getTable()->getName(); $fkIdsColumnName = $fkTableName . '_versions'; return $this->versionTable->getColumn($fkIdsColumnName); }
protected static function getRefRelatedBySuffix(ForeignKey $fk) { $relCol = ''; foreach ($fk->getLocalForeignMapping() as $localColumnName => $foreignColumnName) { $localTable = $fk->getTable(); $localColumn = $localTable->getColumn($localColumnName); if (!$localColumn) { throw new Exception("Could not fetch column: {$columnName} in table " . $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 ($relCol != '') { $relCol = 'RelatedBy' . $relCol; } return $relCol; }
/** * Adds the method that returns the referrer fkey collection. * @param string &$script The script will be modified in this method. */ protected function addRefFKGet(&$script, ForeignKey $refFK) { $table = $this->getTable(); $tblFK = $refFK->getTable(); $fkPeerBuilder = OMBuilder::getNewPeerBuilder($refFK->getTable()); $relCol = $this->getRefFKPhpNameAffix($refFK, $plural = true); $collName = $this->getRefFKCollVarName($refFK); $lastCriteriaName = $this->getRefFKLastCriteriaVarName($refFK); $script .= "\n\t/**\n\t * If this collection has already been initialized with\n\t * an identical criteria, it returns the collection.\n\t * Otherwise if this " . $table->getPhpName() . " has previously\n\t * been saved, it will retrieve related {$relCol} from storage.\n\t * If this " . $table->getPhpName() . " is new, it will return\n\t * an empty collection or the current collection, the criteria\n\t * is ignored on a new object.\n\t *\n\t * @param Connection \$con\n\t * @param Criteria \$criteria\n\t * @throws PropelException\n\t */\n\tpublic function get{$relCol}(\$criteria = null, \$con = null)\n\t{\n\t\t// include the Peer class\n\t\tinclude_once '" . $fkPeerBuilder->getClassFilePath() . "';\n\t\tif (\$criteria === null) {\n\t\t\t\$criteria = new Criteria();\n\t\t}\n\t\telseif (\$criteria instanceof Criteria)\n\t\t{\n\t\t\t\$criteria = clone \$criteria;\n\t\t}\n\n\t\tif (\$this->{$collName} === null) {\n\t\t\tif (\$this->isNew()) {\n\t\t\t \$this->{$collName} = array();\n\t\t\t} else {\n"; foreach ($refFK->getLocalColumns() as $colFKName) { // $colFKName is local to the referring table (i.e. foreign to this table) $lfmap = $refFK->getLocalForeignMapping(); $localColumn = $this->getTable()->getColumn($lfmap[$colFKName]); $colFK = $refFK->getTable()->getColumn($colFKName); $script .= "\n\t\t\t\t\$criteria->add(" . $fkPeerBuilder->getColumnConstant($colFK) . ", \$this->get" . $localColumn->getPhpName() . "());\n"; } // end foreach ($fk->getForeignColumns() $script .= "\n\t\t\t\t" . $fkPeerBuilder->getPeerClassname() . "::addSelectColumns(\$criteria);\n\t\t\t\t\$this->{$collName} = " . $fkPeerBuilder->getPeerClassname() . "::doSelect(\$criteria, \$con);\n\t\t\t}\n\t\t} else {\n\t\t\t// criteria has no effect for a new object\n\t\t\tif (!\$this->isNew()) {\n\t\t\t\t// the following code is to determine if a new query is\n\t\t\t\t// called for. If the criteria is the same as the last\n\t\t\t\t// one, just return the collection.\n"; foreach ($refFK->getLocalColumns() as $colFKName) { // $colFKName is local to the referring table (i.e. foreign to this table) $lfmap = $refFK->getLocalForeignMapping(); $localColumn = $this->getTable()->getColumn($lfmap[$colFKName]); $colFK = $refFK->getTable()->getColumn($colFKName); $script .= "\n\n\t\t\t\t\$criteria->add(" . $fkPeerBuilder->getColumnConstant($colFK) . ", \$this->get" . $localColumn->getPhpName() . "());\n"; } // foreach ($fk->getForeignColumns() $script .= "\n\t\t\t\t" . $fkPeerBuilder->getPeerClassname() . "::addSelectColumns(\$criteria);\n\t\t\t\tif (!isset(\$this->{$lastCriteriaName}) || !\$this->" . $lastCriteriaName . "->equals(\$criteria)) {\n\t\t\t\t\t\$this->{$collName} = " . $fkPeerBuilder->getPeerClassname() . "::doSelect(\$criteria, \$con);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\t\$this->{$lastCriteriaName} = \$criteria;\n\t\treturn \$this->{$collName};\n\t}\n"; }
/** * Adds the method that gets a one-to-one related referrer fkey. * This is for one-to-one relationship special case. * @param string &$script The script will be modified in this method. */ protected function addPKRefFKGet(&$script, ForeignKey $refFK) { $table = $this->getTable(); $tblFK = $refFK->getTable(); $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable()); $joinedTablePeerBuilder = $this->getNewObjectBuilder($refFK->getTable()); $className = $joinedTableObjectBuilder->getObjectClassname(); $varName = $this->getPKRefFKVarName($refFK); $script .= "\n\t/**\n\t * Gets a single {$className} object, which is related to this object by a one-to-one relationship.\n\t *\n\t * @param PropelPDO \$con\n\t * @return {$className}\n\t * @throws PropelException\n\t */\n\tpublic function get" . $this->getRefFKPhpNameAffix($refFK, $plural = false) . "(PropelPDO \$con = null)\n\t{\n"; $script .= "\n\t\tif (\$this->{$varName} === null && !\$this->isNew()) {"; $lfmap = $refFK->getLocalForeignMapping(); // remember: this object represents the foreign table, // so we need foreign columns of the reffk to know the local columns // that we need to set :) $localcols = $refFK->getForeignColumns(); // we know that at least every column in the primary key of the foreign table // is represented in this foreign key $params = array(); foreach ($tblFK->getPrimaryKey() as $col) { $localColumn = $table->getColumn($lfmap[$col->getName()]); $clo = strtolower($localColumn->getName()); $params[] = "\$this->{$clo}"; } $script .= "\n\t\t\t\$this->{$varName} = " . $joinedTableObjectBuilder->getPeerClassname() . "::retrieveByPK(" . implode(", ", $params) . ", \$con);\n\t\t}\n\n\t\treturn \$this->{$varName};\n\t}\n"; }
/** * @param ForeignKey $foreignKey */ public function setIncomingForeignKey($foreignKey) { $this->setMiddleTable($foreignKey ? $foreignKey->getTable() : null); $this->incomingForeignKey = $foreignKey; }
protected function addRefFkGetById(&$script, ForeignKey $refFk) { $foreignPeerBuilder = self::getNewPeerBuilder($refFk->getTable()); $args = array(); foreach ($refFk->getForeignColumns() as $foreignName) { $args[] = "\${$foreignName}"; } $args = implode(', ', $args); $script .= <<<script public static function get{$this->getRefFkPhpNameAffix($refFk, true)}ById({$args}, array \$options = array()) { \$criteria = new Criteria; self::add{$this->getRefFkPhpNameAffix($refFk, true)}CriteriaById(\$criteria, {$args}); return {$foreignPeerBuilder->getPeerClassName()}::get(\$criteria, \$options); } script; }
/** * 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, $reverseOnSelf = false) { $relCol = ''; foreach ($fk->getLocalForeignMapping() as $columnName => $foreignColumnName) { $column = $fk->getTable()->getColumn($columnName); if (!$column) { throw new Exception("Could not fetch column: {$columnName} in table " . $fk->getTable()->getName()); } if (count($column->getTable()->getForeignKeysReferencingTable($fk->getForeignTableName())) > 1) { // if there are several foreign keys that point to the same table // then we need to generate methods like getAuthorRelatedByColName() // instead of just getAuthor(). $relCol .= $column->getPhpName(); } elseif ($fk->getForeignTableName() == $fk->getTable()->getName()) { // self referential foreign key if ($reverseOnSelf) { $relCol .= $column->getPhpName(); } else { $relCol .= $fk->getTable()->getColumn($foreignColumnName)->getPhpName(); } } } if ($relCol != '') { $relCol = 'RelatedBy' . $relCol; } return $relCol; }
/** * Adds the method that sets a one-to-one related referrer fkey. * This is for one-to-one relationships special case. * @param string &$script The script will be modified in this method. * @param ForeignKey $refFK The referencing foreign key. */ protected function addPKRefFKSet(&$script, ForeignKey $refFK) { $tblFK = $refFK->getTable(); $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable()); $className = $joinedTableObjectBuilder->getObjectClassname(); $varName = $this->getPKRefFKVarName($refFK); $script .= " /** * Sets a single $className object as related to this object by a one-to-one relationship. * * @param $className \$l $className * @return ".$this->getObjectClassname()." The current object (for fluent API support) * @throws PropelException */ public function set".$this->getRefFKPhpNameAffix($refFK, $plural = false)."($className \$v) { \$this->$varName = \$v; // Make sure that that the passed-in $className isn't already associated with this object if (\$v->get".$this->getFKPhpNameAffix($refFK, $plural = false)."() === null) { \$v->set".$this->getFKPhpNameAffix($refFK, $plural = false)."(\$this); } return \$this; } "; } // addPKRefFKSet
public function getDropForeignKeyDDL(ForeignKey $fk) { 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())); }
/** * Adds the method that sets a one-to-one related referrer fkey. * This is for one-to-one relationships special case. * @param string &$script The script will be modified in this method. * @param ForeignKey $refFK The referencing foreign key. */ protected function addPKRefFKSet(&$script, ForeignKey $refFK) { $tblFK = $refFK->getTable(); $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable()); $className = $joinedTableObjectBuilder->getObjectClassname(); $varName = $this->getPKRefFKVarName($refFK); $script .= "\r\n\t/**\r\n\t * Sets a single {$className} object as related to this object by a one-to-one relationship.\r\n\t *\r\n\t * @param {$className} \$l {$className}\r\n\t * @return " . $this->getObjectClassname() . " The current object (for fluent API support)\r\n\t * @throws PropelException\r\n\t */\r\n\tpublic function set" . $this->getRefFKPhpNameAffix($refFK, $plural = false) . "({$className} \$v)\r\n\t{\r\n\t\t\$this->{$varName} = \$v;\r\n\r\n\t\t// Make sure that that the passed-in {$className} isn't already associated with this object\r\n\t\tif (\$v->get" . $this->getFKPhpNameAffix($refFK, $plural = false) . "() === null) {\r\n\t\t\t\$v->set" . $this->getFKPhpNameAffix($refFK, $plural = false) . "(\$this);\r\n\t\t}\r\n\r\n\t\treturn \$this;\r\n\t}\r\n"; }
/** * Adds the method that adds an object into the referrer fkey collection. * @param string &$script The script will be modified in this method. */ protected function addCrossFKAdd(&$script, ForeignKey $refFK, ForeignKey $crossFK) { $relCol = $this->getFKPhpNameAffix($crossFK, $plural = true); $collName = $this->getCrossFKVarName($crossFK); $tblFK = $refFK->getTable(); $joinedTableObjectBuilder = $this->getNewObjectBuilder($refFK->getTable()); $className = $joinedTableObjectBuilder->getObjectClassname(); $foreignObjectName = '$' . $tblFK->getStudlyPhpName(); $crossObjectName = '$' . $crossFK->getForeignTable()->getStudlyPhpName(); $crossObjectClassName = $this->getNewObjectBuilder($crossFK->getForeignTable())->getObjectClassname(); $script .= "\n\t/**\n\t * Associate a " . $crossObjectClassName . " object to this object\n\t * through the " . $tblFK->getName() . " cross reference table.\n\t *\n\t * @param " . $crossObjectClassName . " " . $crossObjectName . " The {$className} object to relate\n\t * @return void\n\t */\n\tpublic function add" . $this->getFKPhpNameAffix($crossFK, $plural = false) . "(" . $crossObjectName . ")\n\t{\n\t\tif (\$this->" . $collName . " === null) {\n\t\t\t\$this->init" . $relCol . "();\n\t\t}\n\t\tif (!\$this->" . $collName . "->contains(" . $crossObjectName . ")) { // only add it if the **same** object is not already associated\n\t\t\t" . $foreignObjectName . " = new " . $className . "();\n\t\t\t" . $foreignObjectName . "->set" . $this->getFKPhpNameAffix($crossFK, $plural = false) . "(" . $crossObjectName . ");\n\t\t\t\$this->add" . $this->getRefFKPhpNameAffix($refFK, $plural = false) . "(" . $foreignObjectName . ");\n\n\t\t\t\$this->" . $collName . "[]= " . $crossObjectName . ";\n\t\t}\n\t}\n"; }
public function getReferrerVersionsColumn(ForeignKey $fk) { $fkTableName = $fk->getTable()->getName(); if ($fk->isLocalPrimaryKey()) { $fkColumnName = $fkTableName . '_version'; } else { $fkColumnName = $fkTableName . '_versions'; } return $this->versionTable->getColumn($fkColumnName); }