0

I have run through an unexpected behaviour while using Doctrine 1.2.4 with Symfony: accessing the records via the getters did not lead to the same result as shooting a query to retrieve these same records. Here is how it goes:

I am working with two tables: a tpTrip table, on which we perform actions(= status changes), and a tpTripActivity table, that keeps track of all the actions that have been performed on the trip. tpTrip has a one to many relationship pointing to tpTripActivity.

At some point, I wanted to know if a "CheckIn" action had been performed on a tpTrip record. So in my Doctrine tpTrip class, I wrote the (naive) method:

public function has_been_checked_in()
{               
    foreach($this->getActivities() as $activity)
    {           
        if($activity->getAction() == "CheckIn") return true;
    }
    return false;
}

Now after some frustrations, observing that a recently performed CheckIn action was not turning up in my results, I wrote the following function, which I would expect to do the same:

public function has_been_checked_in2()
{               
        $total = Doctrine_Core::getTable("tpTripActivity")->createQuery("a")
        ->select("count(*)")
        ->where("trip_id = ?", $this->getId())
        ->andWhere("action = ?", "CheckIn")
        ->fetchOne(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);

    return ($total > 0);
}

In this second version, I could get consistent results.

So my question is: Why do I not get the same results with the two functions ? How can I make sure that my records are up to date when I use my foreign key getters ?

mika
  • 1,971
  • 3
  • 18
  • 32

1 Answers1

0

Doctrine permits to refresh records' relashionships, as mentionned in the doc. This would go as follow:

$this->refreshRelated();
foreach($this->getActivities() as $activity){
    ....
}

See also this post on stack overflow.

Community
  • 1
  • 1
mika
  • 1,971
  • 3
  • 18
  • 32