Skip to content
This repository
Browse code

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
William Durand willdurand authored
29 generator/lib/builder/om/QueryBuilder.php
@@ -186,6 +186,7 @@ protected function addClassBody(&$script)
186 186 $this->addConstructor($script);
187 187 $this->addFactory($script);
188 188 $this->addFindPk($script);
  189 + $this->addFindOneByPk($script);
189 190 $this->addFindPkSimple($script);
190 191 $this->addFindPkComplex($script);
191 192 $this->addFindPks($script);
@@ -442,6 +443,34 @@ public function findPk(\$key, \$con = null)
442 443 ";
443 444 }
444 445
  446 + protected function addFindOneByPk(&$script)
  447 + {
  448 + $table = $this->getTable();
  449 + if ($table->hasCompositePrimaryKey()) {
  450 + return;
  451 + }
  452 +
  453 + $pk = $table->getPrimaryKey();
  454 + $column = $pk[0]->getPhpName();
  455 + $ARClassname = $this->getObjectClassname();
  456 +
  457 + $script .= "
  458 + /**
  459 + * Alias of findPk to use instance pooling
  460 + *
  461 + * @param mixed \$key Primary key to use for the query
  462 + * @param PropelPDO \$con A connection object
  463 + *
  464 + * @return $ARClassname A model object, or null if the key is not found
  465 + * @throws PropelException
  466 + */
  467 + public function findOneBy{$column}(\$key, \$con = null)
  468 + {
  469 + return \$this->findPk(\$key, \$con);
  470 + }
  471 +";
  472 + }
  473 +
445 474 protected function addFindPkSimple(&$script)
446 475 {
447 476 $table = $this->getTable();
21 test/testsuite/generator/builder/om/QueryBuilderTest.php
@@ -128,6 +128,12 @@ public function testFindPk()
128 128 $this->assertEquals('BaseTable4Query', $method->getDeclaringClass()->getName(), 'BaseQuery overrides findPk()');
129 129 }
130 130
  131 + public function testFindOneById()
  132 + {
  133 + $method = new ReflectionMethod('Table4Query', 'findOneById');
  134 + $this->assertEquals('BaseTable4Query', $method->getDeclaringClass()->getName(), 'BaseQuery overrides findOneById()');
  135 + }
  136 +
131 137 public function testFindPkReturnsCorrectObjectForSimplePrimaryKey()
132 138 {
133 139 $b = new Book();
@@ -200,6 +206,21 @@ public function testFindPkSimpleAddsObjectToInstancePool()
200 206 $this->assertEquals($count, $this->con->getQueryCount());
201 207 }
202 208
  209 + public function testFindOneByIdAddsObjectToInstancePool()
  210 + {
  211 + $b = new Book();
  212 + $b->setTitle('foo');
  213 + $b->save($this->con);
  214 + BookPeer::clearInstancePool();
  215 +
  216 + BookQuery::create()->findOneById($b->getId(), $this->con);
  217 + $count = $this->con->getQueryCount();
  218 +
  219 + $book = BookQuery::create()->findOneById($b->getId(), $this->con);
  220 + $this->assertEquals($b, $book);
  221 + $this->assertEquals($count, $this->con->getQueryCount());
  222 + }
  223 +
203 224 public function testFindPkUsesFindPkComplexOnNonEmptyQueries()
204 225 {
205 226 BookQuery::create('b')->findPk(123, $this->con);

0 comments on commit 8b02639

Please sign in to comment.
Something went wrong with that request. Please try again.