4
votes

I'm using Doctrine 1.2 and Symfony 1.4.

In my action, I have two different query that return different result set. Somehow the second query seem to change the result (or the reference?) of the first one and I don't have any clue why..

Here is an example:

  $this->categories = Doctrine_Query::create()
       ->from('Categorie AS c')
       ->innerJoin('c.Activite AS a')
       ->where('a.archive = ?', false)
       ->execute();

  print_r($this->categories->toArray()); // Return $this->categories results, normal behavior.

  $this->evil_query = Doctrine_Query::create()
       ->from('Categorie AS c')
       ->innerJoin('c.Activite AS a')
       ->where('a.archive = ?', true)
       ->execute();

  print_r($this->categories->toArray()); // Should be the same as before, but it return $this->evil_query results instead!

Why Doctrine behave this way ? It's totally driving me crazy. Thanks!

To make it simple it seem like the Query 2 are hijacking the Query 1 result.

5
Can you show the results? What happens if you store the results of toArray(), rather than calling it on Doctrine_Collection?Jeremy Kauffman
jeremy: If I store the result of the toArray(), everything starts working as expected.Hubert Perron

5 Answers

5
votes

Use something like this between queries ($em - entity manager):

$em->clear(); // Detaches all objects from Doctrine!

http://docs.doctrine-project.org/en/2.0.x/reference/batch-processing.html

2
votes

In the API docs for the toArray() method in Doctrine_Collection it says:

Mimics the result of a $query->execute(array(), Doctrine_Core::HYDRATE_ARRAY);

I suspect to answer this question to your satisfaction you're going to have to go through the source code.

0
votes

This seems like it has to be an issue of $this->categories and $this->evil_query pointing to the same place. What are the results of $this->evil_query === $this->categories and $this->evil_query == $this->categories?

0
votes

Hubert, have you tried storing the query into a separate variable and then calling the execute() method on it?

I mean something like:

$good_query = Doctrine_Query::create()
  ->from('...')
  ->innerJoin('...')
  ->where('...', false)
;
$evil_query = Doctrine_Query::create()
  ->from('...')
  ->innerJoin('...')
  ->where('...', true)
;
$this->categories = $good_query->execute();
$this->evil_query = $evil_query->execute();

It seems like both attributes (categories and evil_query) are pointing at the same object.

-1
votes

Erm, after both queries you print_r the result of the first query - change the last line to print_r($this->evil_query->toArray()); to see the difference :)