简体   繁体   English

原则-创建多个查询

[英]Doctrine - create multiple queries

I'm trying to create multiples queries in Doctrine. 我正在尝试在Doctrine中创建多个查询。 I create two variables, each one begin with the same query. 我创建两个变量,每个变量都以相同的查询开头。 Then I want to have two distincts queries. 然后,我想有两个不同的查询。 But if I modify the first query, the second one get the changes, acting like pointers. 但是,如果我修改第一个查询,第二个查询将获得更改,就像指针一样。

I use Symfony 3.0 , Doctrine 2.4.8 我使用Symfony 3.0,Doctrine 2.4.8

$queryA = $queryB = $em->getRepository('MyBundle:MyEntity')
            ->createQueryBuilder('me')
            ->join('me.foo', 'f')
            ->where('me.status = :valid')
            ->andWhere('foo.bar = :bar')
            ->setParameters([
                'valid' => 'valid',
                'bar' => 'bar',
            ]);

dump($queryB);

$results = $queryA
    ->join('me.lol', 'lol')
    ->getQuery()->getResult();

dump($queryB);exit;

Now the $queryB has the join me.lol . 现在,$ queryB具有me.lol I know this is caused by the $queryA = $queryB. 我知道这是由$ queryA = $ queryB引起的。 But I want to factor my code. 但是我想考虑我的代码。

How can I have two separates variables with the same query ? 如何在同一查询中使用两个单独的变量? When I use a join with the $queryA doesn't affect the $queryB ? 当我对$ queryA使用联接时,不会影响$ queryB吗?

Thanks ! 谢谢 !

克隆$queryA ,克隆将为您提供独特的queryB对象,该对象与queryA相同,但没有对queryA的引用:

$queryB = clone $queryA;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM