Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #441 from cedriclombardot/feat-findOneById-alias

Make findOneById to be an alias of findPk
  • Loading branch information...
commit 8b026396ef595c6a47deec3c9d940f005fe7ca30 2 parents d48359e + 737bb32
@willdurand willdurand authored
View
29 generator/lib/builder/om/QueryBuilder.php
@@ -186,6 +186,7 @@ protected function addClassBody(&$script)
$this->addConstructor($script);
$this->addFactory($script);
$this->addFindPk($script);
+ $this->addFindOneByPk($script);
$this->addFindPkSimple($script);
$this->addFindPkComplex($script);
$this->addFindPks($script);
@@ -442,6 +443,34 @@ public function findPk(\$key, \$con = null)
";
}
+ protected function addFindOneByPk(&$script)
+ {
+ $table = $this->getTable();
+ if ($table->hasCompositePrimaryKey()) {
+ return;
+ }
+
+ $pk = $table->getPrimaryKey();
+ $column = $pk[0]->getPhpName();
+ $ARClassname = $this->getObjectClassname();
+
+ $script .= "
+ /**
+ * Alias of findPk to use instance pooling
+ *
+ * @param mixed \$key Primary key to use for the query
+ * @param PropelPDO \$con A connection object
+ *
+ * @return $ARClassname A model object, or null if the key is not found
+ * @throws PropelException
+ */
+ public function findOneBy{$column}(\$key, \$con = null)
+ {
+ return \$this->findPk(\$key, \$con);
+ }
+";
+ }
+
protected function addFindPkSimple(&$script)
{
$table = $this->getTable();
View
21 test/testsuite/generator/builder/om/QueryBuilderTest.php
@@ -128,6 +128,12 @@ public function testFindPk()
$this->assertEquals('BaseTable4Query', $method->getDeclaringClass()->getName(), 'BaseQuery overrides findPk()');
}
+ public function testFindOneById()
+ {
+ $method = new ReflectionMethod('Table4Query', 'findOneById');
+ $this->assertEquals('BaseTable4Query', $method->getDeclaringClass()->getName(), 'BaseQuery overrides findOneById()');
+ }
+
public function testFindPkReturnsCorrectObjectForSimplePrimaryKey()
{
$b = new Book();
@@ -200,6 +206,21 @@ public function testFindPkSimpleAddsObjectToInstancePool()
$this->assertEquals($count, $this->con->getQueryCount());
}
+ public function testFindOneByIdAddsObjectToInstancePool()
+ {
+ $b = new Book();
+ $b->setTitle('foo');
+ $b->save($this->con);
+ BookPeer::clearInstancePool();
+
+ BookQuery::create()->findOneById($b->getId(), $this->con);
+ $count = $this->con->getQueryCount();
+
+ $book = BookQuery::create()->findOneById($b->getId(), $this->con);
+ $this->assertEquals($b, $book);
+ $this->assertEquals($count, $this->con->getQueryCount());
+ }
+
public function testFindPkUsesFindPkComplexOnNonEmptyQueries()
{
BookQuery::create('b')->findPk(123, $this->con);
Please sign in to comment.
Something went wrong with that request. Please try again.