Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

One-to-one relation setter method cause unnecessary queries #476

Closed
brunnels opened this Issue · 1 comment

2 participants

@brunnels

Example:

***** BasesfGuardUser *******

  /**
   * Sets a single sfGuardUserProfile object as related to this object by a one-to-one relationship.
   *
   * @param      sfGuardUserProfile $v sfGuardUserProfile
   * @return     sfGuardUser The current object (for fluent API support)
   * @throws     PropelException
   */
  public function setsfGuardUserProfile(sfGuardUserProfile $v = null)
  {
    $this->singlesfGuardUserProfile = $v;

    // Make sure that that the passed-in sfGuardUserProfile isn't already associated with this object
    if ($v !== null && $v->getsfGuardUser() === null)
    {
      $v->setsfGuardUser($this);
    }

    return $this;
  }
***** BasesfGuardUserProfile *******
  /**
   * Get the associated sfGuardUser object
   *
   * @param      PropelPDO Optional Connection object.
   * @return     sfGuardUser The associated sfGuardUser object.
   * @throws     PropelException
   */
  public function getsfGuardUser(PropelPDO $con = null)
  {
    if ($this->asfGuardUser === null && ($this->user_id !== null))
    {
      $this->asfGuardUser = sfGuardUserQuery::create()->findPk($this->user_id, $con);
      // Because this foreign key represents a one-to-one relationship, we will create a bi-directional association.
      $this->asfGuardUser->setsfGuardUserProfile($this);
    }
    return $this->asfGuardUser;
  }

This will cause a query to get the same sfGuardUser object that is calling it during object hydration. Easiest solution I can think of since sfGuardUserProfile->asfGuardUser is protected is to add an option to the generated sfGuardUserProfile::getsfGuardUser() method to not execute the query. Like so:

  /**
   * Get the associated sfGuardUser object
   *
   * @param      PropelPDO Optional Connection object.
   * @param      $doQuery Executes a query to get the object if required
   * @return     sfGuardUser The associated sfGuardUser object.
   * @throws     PropelException
   */
  public function getsfGuardUser(PropelPDO $con = null, $doQuery = true)
  {
    if ($this->asfGuardUser === null && ($this->user_id !== null) && $doQuery)
    {
      $this->asfGuardUser = sfGuardUserQuery::create()->findPk($this->user_id, $con);
      // Because this foreign key represents a one-to-one relationship, we will create a bi-directional association.
      $this->asfGuardUser->setsfGuardUserProfile($this);
    }
    return $this->asfGuardUser;
  }

Then in the BasesfGuardUser::setsfGuardUserProfile() change to getsfGuardUser(null, false);

@brunnels

I'll issue a pull request for the change tomorrow. This is also causing me issues with php version > 5.3.3 pdo_dblib driver. It no longer allows to issue queries while there is still data to be read from the prior query. This is giving the dreaded "pending results" error message. See this FAQ http://freetds.schemamania.org/faq.html#pending

It's very strange that this only started happening when I upgraded from php 5.3.3 to 5.3.17 as I see nothing in the php change logs that would account for it.

@willdurand willdurand closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.