2013-07-31 15 views
5

mam tej kwerendy z podzapytania:Błąd podzapytaniu kreator zapytań 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(); 

i dostaję ten błąd:

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

nie mogę znaleźć przyczynę błędu, alias f jest zdefiniowany tylko jeden raz. Jakieś sugestie?

Odpowiedz

7

Ten numer dotyczy obiektów i odniesień w PHP.

Kiedy robisz $subquery = $query;, $query będąc obiektem, po prostu masz $subquery wskazującą tę samą wartość.

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.

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

Oznacza to w kodzie, że kiedy piszesz tak:

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

Jest to odpowiednik:

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

Więc kiedy w końcu ty zadzwoń pod numer:

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

Używasz 2 razy tego samego obiektu wskazanego przez 2 różne zmienne ($query === $subquery).

Aby rozwiązać ten problem, można albo zastosowanie:

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

Albo clone kluczowe:

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

Dziękuję bardzo, ale teraz błąd: [Błąd semantyczny] linia 0, col 74 w pobliżu "następujący FROM": Błąd: Niepoprawna PathExpression. Musi to być StateFieldPathExpression. – m4t1t0

+1

@ m4t1t0 może to pomóc: http://stackoverflow.com/questions/14216470/symfony2-and-doctrine-error-invalid-pathexpress-must-be-a-statefieldpathe – cheesemacfly

+0

Tak, to mi pomaga, dziękuję! – m4t1t0

1

chciałbym podzielić się moim rozwiązanie, które wymaga ORM mapowanie:

Following jednostki są mapowane w ten sposób: Zdarzenie 1: Uczestnik 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) 
     ; 
} 

} 
Powiązane problemy