Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Fix for one-to-one related object have unnecessary queries during hydration #483

merged 3 commits into from

2 participants


This is to resolve #476


Heya @brunnels, thank you for this patch. It looks good but could you add some unit tests before I merge this PR?


@willdurand, What should we test? That an additional query doesn't happen during hydration of an object with a one-to-one relationship? I'm not sure how to test this. Is there a way in the tests to count how many queries are run during a single query->find()? I ran the ModelWithTest.php tests and it still passes.

@willdurand willdurand merged commit e9ef7b1 into propelorm:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 3 deletions.
  1. +4 −3 generator/lib/builder/om/PHP5ObjectBuilder.php
7 generator/lib/builder/om/PHP5ObjectBuilder.php
@@ -3250,13 +3250,14 @@ protected function addFKAccessor(&$script, ForeignKey $fk)
* Get the associated $className object
* @param PropelPDO \$con Optional Connection object.
+ * @param \$doQuery Executes a query to get the object if required
* @return $className The associated $className object.
* @throws PropelException
- public function get".$this->getFKPhpNameAffix($fk, $plural = false)."(PropelPDO \$con = null)
+ public function get".$this->getFKPhpNameAffix($fk, $plural = false)."(PropelPDO \$con = null, \$doQuery = true)
$script .= "
- if (\$this->$varName === null && ($conditional)) {";
+ if (\$this->$varName === null && ($conditional) && \$doQuery) {";
if ($useRetrieveByPk) {
$script .= "
\$this->$varName = ".$fkQueryBuilder->getClassname()."::create()->findPk($localColumns, \$con);";
@@ -3952,7 +3953,7 @@ public function set".$this->getRefFKPhpNameAffix($refFK, $plural = false)."($cla
\$this->$varName = \$v;
// Make sure that that the passed-in $className isn't already associated with this object
- if (\$v !== null && \$v->get".$this->getFKPhpNameAffix($refFK, $plural = false)."() === null) {
+ if (\$v !== null && \$v->get".$this->getFKPhpNameAffix($refFK, $plural = false)."(null, false) === null) {
\$v->set".$this->getFKPhpNameAffix($refFK, $plural = false)."(\$this);
Something went wrong with that request. Please try again.