Permalink
Browse files

[Generator][Peer] remove Peer::retrieveByPk* and use Query::FindPk*

  • Loading branch information...
1 parent b815791 commit 6497478b23876a0365bc967b7c3b3cce1efc7911 @jaugustin jaugustin committed Jan 27, 2013
@@ -28,6 +28,8 @@ class SortableBehaviorPeerBuilderModifier
protected $peerClassName;
+ protected $queryClassName;
+
protected $tableMapClassName;
public function __construct($behavior)
@@ -61,6 +63,7 @@ protected function setBuilder($builder)
$this->builder = $builder;
$this->objectClassName = $builder->getObjectClassName();
$this->peerClassName = $builder->getPeerClassName();
+ $this->queryClassName = $builder->getQueryClassName();
$this->tableMapClassName = $builder->getTableMapClassName();
}
@@ -163,7 +166,7 @@ static public function retrieveByRank(\$rank, " . ($useScope ? "\$scope = null,
protected function addReorder(&$script)
{
- $peerClassName = $this->peerClassName;
+ $queryClassName = $this->queryClassName;
$columnGetter = 'get' . $this->behavior->getColumnForParameter('rank_column')->getPhpName();
$columnSetter = 'set' . $this->behavior->getColumnForParameter('rank_column')->getPhpName();
$script .= "
@@ -186,7 +189,7 @@ static public function reorder(array \$order, ConnectionInterface \$con = null)
\$con->beginTransaction();
try {
\$ids = array_keys(\$order);
- \$objects = $peerClassName::retrieveByPKs(\$ids);
+ \$objects = $queryClassName::create()->findPKs(\$ids);
foreach (\$objects as \$object) {
\$pk = \$object->getPrimaryKey();
if (\$object->$columnGetter() != \$order[\$pk]) {
@@ -113,20 +113,6 @@ protected function addUpdateMethods(&$script)
}
/**
- * Adds the retrieveByPK() (and possibly retrieveByPKs()) method(s) appropriate for this class.
- * @param string &$script The script will be modified in this method.
- */
- protected function addRetrieveByPKMethods(&$script)
- {
- if (1 === count($this->getTable()->getPrimaryKey())) {
- $this->addRetrieveByPK_SinglePK($script);
- $this->addRetrieveByPKs_SinglePK($script);
- } else {
- $this->addRetrieveByPK_MultiPK($script);
- }
- }
-
- /**
* This method adds the contents of the generated class to the script.
*
* This method contains the high-level logic that determines which methods
@@ -162,10 +148,6 @@ protected function addClassBody(&$script)
if (!$table->isAlias() && !$table->isReadOnly()) {
$this->addUpdateMethods($script);
}
-
- if (count($table->getPrimaryKey()) > 0) {
- $this->addRetrieveByPKMethods($script);
- }
}
/**
@@ -231,23 +213,6 @@ protected function isAddGenericAccessors()
}
/**
- * Returns the retrieveByPK method name to use for this table.
- * If the table is an alias then the method name looks like "retrieveTablenameByPK"
- * otherwise simply "retrieveByPK".
- * @return string
- */
- public function getRetrieveMethodName()
- {
- if ($this->getTable()->isAlias()) {
- $retrieveMethod = sprint('retrieve%sByPK', $this->getTable()->getPhpName());
- } else {
- $retrieveMethod = 'retrieveByPK';
- }
-
- return $retrieveMethod;
- }
-
- /**
* COMPATIBILITY: Get the column constant name (e.g. PeerName::COLUMN_NAME).
*
* This method exists simply because it belonged to the 'PeerBuilder' that this
@@ -3167,9 +3167,9 @@ protected function addFKAccessor(&$script, ForeignKey $fk)
$localColumns = array(); // foreign key local attributes names
// If the related columns are a primary key on the foreign table
- // then use retrieveByPk() instead of doSelect() to take advantage
+ // then use findPk() instead of doSelect() to take advantage
// of instance pooling
- $useRetrieveByPk = $fk->isForeignPrimaryKey();
+ $findPk = $fk->isForeignPrimaryKey();
foreach ($fk->getLocalColumns() as $columnName) {
@@ -3210,7 +3210,7 @@ public function get".$this->getFKPhpNameAffix($fk, false)."(ConnectionInterface
{";
$script .= "
if (\$this->$varName === null && ($conditional)) {";
- if ($useRetrieveByPk) {
+ if ($findPk) {
$script .= "
\$this->$varName = ".$this->getClassNameFromBuilder($fkQueryBuilder)."::create()->findPk($localColumns, \$con);";
} else {
@@ -656,7 +656,7 @@ protected function addAddInstanceToPool(&$script)
* from the database. In some cases -- especially when you override doSelect*()
* methods in your stub classes -- you may need to explicitly add objects
* to the cache in order to ensure that the same objects are always returned by doSelect*()
- * and retrieveByPK*() calls.
+ * and findPk*() calls.
*
* @param ".$this->getObjectClassName()." \$obj A ".$this->getObjectClassName()." object.
* @param string \$key (optional) key to use for instance map (for performance boost if key was already calculated externally).
@@ -1666,137 +1666,6 @@ protected static function doOnDeleteSetNull(Criteria \$criteria, ConnectionInter
}
/**
- * Adds the retrieveByPK method for tables with single-column primary key.
- * @param string &$script The script will be modified in this method.
- */
- protected function addRetrieveByPK_SinglePK(&$script)
- {
- $table = $this->getTable();
- $pks = $table->getPrimaryKey();
- $col = $pks[0];
-
- $script .= "
- /**
- * Retrieve a single object by pkey.
- *
- * @param ".$col->getPhpType()." \$pk the primary key.
- * @param ConnectionInterface \$con the connection to use
- * @return ".$this->getObjectClassName()."
- */
- static public function ".$this->getRetrieveMethodName()."(\$pk, ConnectionInterface \$con = null)
- {
-
- if (null !== (\$obj = static::getInstanceFromPool(".$this->getInstancePoolKeySnippet('$pk')."))) {
- return \$obj;
- }
-
- if (null === \$con) {
- \$con = Propel::getServiceContainer()->getReadConnection(" . $this->getTableMapClass() . "::DATABASE_NAME);
- }
-
- \$criteria = new Criteria(" . $this->getTableMapClass() . "::DATABASE_NAME);
- \$criteria->add(".$this->getColumnConstant($col).", \$pk);
-
- \$v = static::doSelect(\$criteria, \$con);
-
- return !empty(\$v) > 0 ? \$v[0] : null;
- }
-";
- }
-
- /**
- * Adds the retrieveByPKs method for tables with single-column primary key.
- * @param string &$script The script will be modified in this method.
- */
- protected function addRetrieveByPKs_SinglePK(&$script)
- {
- $table = $this->getTable();
- $script .= "
- /**
- * Retrieve multiple objects by pkey.
- *
- * @param array \$pks List of primary keys
- * @param ConnectionInterface \$con the connection to use
- * @throws PropelException Any exceptions caught during processing will be
- * rethrown wrapped into a PropelException.
- */
- static public function ".$this->getRetrieveMethodName()."s(\$pks, ConnectionInterface \$con = null)
- {
- if (null === \$con) {
- \$con = Propel::getServiceContainer()->getReadConnection(" . $this->getTableMapClass() . "::DATABASE_NAME);
- }
-
- \$objs = null;
- if (empty(\$pks)) {
- \$objs = array();
- } else {
- \$criteria = new Criteria(" . $this->getTableMapClass() . "::DATABASE_NAME);";
- $k1 = $table->getPrimaryKey();
- $script .= "
- \$criteria->add(".$this->getColumnConstant($k1[0]).", \$pks, Criteria::IN);";
- $script .= "
- \$objs = static::doSelect(\$criteria, \$con);
- }
-
- return \$objs;
- }
-";
- }
-
- /**
- * Adds the retrieveByPK method for tables with multi-column primary key.
- * @param string &$script The script will be modified in this method.
- */
- protected function addRetrieveByPK_MultiPK(&$script)
- {
- $table = $this->getTable();
- $script .= "
- /**
- * Retrieve object using using composite pkey values.";
- foreach ($table->getPrimaryKey() as $col) {
- $clo = strtolower($col->getName());
- $cptype = $col->getPhpType();
- $script .= "
- * @param $cptype $".$clo;
- }
- $script .= "
- * @param ConnectionInterface \$con
- * @return ".$this->getObjectClassName()."
- */
- static public function ".$this->getRetrieveMethodName()."(";
-
- $php = array();
- foreach ($table->getPrimaryKey() as $col) {
- $clo = strtolower($col->getName());
- $php[] = '$' . $clo;
- } /* foreach */
-
- $script .= implode(', ', $php);
-
- $script .= ", ConnectionInterface \$con = null) {
- \$_instancePoolKey = ".$this->getInstancePoolKeySnippet($php).";";
- $script .= "
- if (null !== (\$obj = static::getInstanceFromPool(\$_instancePoolKey))) {
- return \$obj;
- }
-
- if (null === \$con) {
- \$con = Propel::getServiceContainer()->getReadConnection(" . $this->getTableMapClass() . "::DATABASE_NAME);
- }
- \$criteria = new Criteria(" . $this->getTableMapClass() . "::DATABASE_NAME);";
- foreach ($table->getPrimaryKey() as $col) {
- $clo = strtolower($col->getName());
- $script .= "
- \$criteria->add(".$this->getColumnConstant($col).", $".$clo.");";
- }
- $script .= "
- \$v = static::doSelect(\$criteria, \$con);
-
- return !empty(\$v) ? \$v[0] : null;
- }";
- }
-
- /**
* Adds the getTableMap() method which is a convenience method for apps to get DB metadata.
* @param string &$script The script will be modified in this method.
*/

0 comments on commit 6497478

Please sign in to comment.