开发者

Doctrine ORM, two different querys produce the same result set

开发者 https://www.devze.com 2023-01-23 09:53 出处:网络
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

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.


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


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.


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?


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.


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消