2013-07-31 44 views
5

我有這個查詢與子查詢:錯誤與doctrine2查詢生成器子查詢

$query = $this->getEntityManager()->createQueryBuilder(); 
    $subquery = $query; 
    $subquery 
     ->select('f.following') 
     ->from('ApiBundle:Follow', 'f') 
     ->where('f.follower = :follower_id') 
     ->setParameter('follower_id', $id) 
    ; 

    $query 
     ->select('c') 
     ->from('ApiBundle:Chef', 'c') 
     ->where('c.id <> :id') 
     ->setParameter('id', $id) 
    ; 
    $query 
     ->andWhere(
      $query->expr()->notIn('c.id', $subquery->getDQL()) 
     ); 

    return $query->getQuery()->getResult(); 

而且我得到這個錯誤:

[Semantical Error] line 0, col 116 near 'f, ApiBundle:Chef': Error: 'f' is already defined. 

我無法找到錯誤的原因,別名f只定義一次。有什麼建議麼?

回答

7

此問題與PHP中的對象和引用有關。

當你做$subquery = $query;$query是一個對象時,你只需要$subquery指向相同的值。

A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is [...] assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

參考:http://us1.php.net/manual/en/language.oop5.references.php

這意味着在你的代碼,當你這樣寫:

$subquery 
    ->select('f.following') 
    ->from('ApiBundle:Follow', 'f') 
    ->where('f.follower = :follower_id') 
    ->setParameter('follower_id', $id) 
; 

這相當於:

$query 
    ->select('f.following') 
    ->from('ApiBundle:Follow', 'f') 
    ->where('f.follower = :follower_id') 
    ->setParameter('follower_id', $id) 
; 

末你所以,當電話:

$query->andWhere(
     $query->expr()->notIn('c.id', $subquery->getDQL()) 
    ); 

您使用2次由2個不同變量($query === $subquery)指向的相同對象。

爲了解決這個問題,你可以使用:

$query = $this->getEntityManager()->createQueryBuilder(); 
$subquery = $this->getEntityManager()->createQueryBuilder(); 

還是clone關鍵字:

$query = $this->getEntityManager()->createQueryBuilder(); 
$subquery = clone $query; 
+0

非常感謝,但是現在的錯誤是:[語義錯誤]第0行,第74行'FROM FROM'後面:錯誤:無效PathExpression。必須是StateFieldPathExpression。 – m4t1t0

+1

@ m4t1t0可以幫助:http://stackoverflow.com/questions/14216470/symfony2-and-doctrine-error-invalid-pathexpression-must-be-a-statefieldpathe – cheesemacfly

+0

是的,它可以幫助我,謝謝! – m4t1t0

1

我想和大家分享我的解決方案,它需要ORM映射:

繼實體映射如下: 事件1:M參與者

Participant class 
/** 
* @ORM\ManyToOne(targetEntity="KKB\TestBundle\Entity\Event", inversedBy="participants") 
* @ORM\JoinColumn(name="event_id", referencedColumnName="id", nullable=false) 
*/ 
private $event; 

Event class 
/** 
* @ORM\OneToMany(targetEntity="KKB\TestBundle\Entity\Participant", mappedBy="event", cascade={"persist"}) 
*/ 
private $participants; 


class EventRepository extends \Doctrine\ORM\EntityRepository 
{ 

public function getEventList($userId) 
{ 

    $query = $this->createQueryBuilder('e'); 
    $subquery = $this->createQueryBuilder('se'); 

    $subquery 
     ->leftJoin('se.participants', 'p') 
     ->where('p.user = :userId') 
     ; 

    return $query->where($query->expr()->notIn('e.id', $subquery->getDQL())) 
     ->setParameter('userId', $userId) 
     ; 
} 

}