2013-03-02 17 views
5

próbuję odfiltrować dzieci, gdzie rodzic składa się ze złożonego klucza głównego, rodzic nazywany jest przedmiotem, a dzieci są kursy:Doctrine 2 klucze kompozytowych i DQL dołącza

Temat:

class Subject 
{ 
    /** 
    * @var integer 
    * 
    * @Column(type="integer") 
    * @Id 
    * @GeneratedValue(strategy="NONE") 
    */ 
    protected $id; 

    /** 
    * @var integer 
    * 
    * @Column(type="integer") 
    * @Id 
    */ 
    protected $customer_id; 

    /** 
    * @var ArrayCollection 
    * 
    * @OneToMany(targetEntity="Course", mappedBy="subject") 
    */ 
    private $courses; 

    ... 
} 

przedmiotu:

class Course 
{ 
    ... 

    /** 
    * @var Subject 
    * 
    * @ManyToOne(targetEntity="Subject", inversedBy="courses") 
    * @JoinColumns({ 
    * @JoinColumn(name="subject_id", referencedColumnName="id"), 
    * @JoinColumn(name="subject_customer_id", referencedColumnName="customer_id") 
    * }) 
    */ 
    private $subject; 
} 

mam "subject_id" i "subject_customer_id", moim problemem jest to, że nie można odfiltrować kursy bez łączenia temat, kiedy piszę te słowa:

$this->em->createQuery("SELECT c FROM Course c WHERE c.subject = :subject") 
    ->setParameters(array('subject' => array('subject_id' => $subject_id, 'subject_customer_id' => $subject_customer_id))) 
    ->getResult(); 

pojawia się następujący błąd:

Doctrine\ORM\Query\QueryException [ 0 ]: A single-valued association path expression to an entity with a composite primary key is not supported. Explicitly name the components of the composite primary key in the query.

Tylko dlatego mogę dostać ten robocza jest przez wewnętrzną łączącą tematu, przykład:

$this->em->createQuery("SELECT c FROM Course c INNER JOIN c.subject s WITH s.id = :subject AND s.customer_id = :customer") 
    ->setParameters(array('subject' => $subject_id, 'customer' => $customer_id)) 
    ->getResult(); 

Czy istnieje jakiś sposób, aby odzyskać kursy w oparciu o przedmiotowe klucze podstawowe bez dołączania do tematu?

Odpowiedz

2

Od daty bieżącej ta logika to not yet supported by DQL (patrz także: https://github.com/doctrine/doctrine2/blob/2.3.2/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php#L1641-L1643).

trzeba będzie połączyć się z czymś takim:

SELECT 
    c 
FROM 
    Course c 
INNER JOIN 
    c.subject s 
WHERE 
    s.id = :subject 
    AND 
    s.customer_id = :customer 

Rozważmy przyczyniając się do projektu poprzez dodanie takiej logiki w końcu: to może być połączone w nowej serii 2.4.