2011-09-19 2 views
2

저는 제품과 색상 사이에 많은 관계가 있습니다.Doctrine Entity findBy Many to Many

내가하려는 일은 색상별로 제품을 찾는 것입니다.

예)

$colours = $em->getRepository('Xxxxx\XxxxxBundle\Entity\Colour')->findBy(array('name'=>'red'); 
$products = $em->getRepository('Xxxxx\XxxxxBundle\Entity\Product')->findBy(array('colours'=>$colours)); 

이 내 YAML의 설정입니다 :

Xxxxx\XxxxxBundle\Entity\Product: 
    type: entity 
    manyToMany: 
    colours: 
     targetEntity: Colour 
     joinTable: 
     name: Product_Colour 
     joinColumns: 
      product_id: 
      referencedColumnName: id 
     inverseJoinColumns: 
      colour_id: 
      referencedColumnName: id 

.

Xxxxx\XxxxxBundle\Entity\Colour: 
    type: entity 
    id: 
    id: 
     type: integer 
     generator: 
     strategy: AUTO 
    fields: 
    hex: 
     type: string 
     length: 320 
    name: 
     type: string 
     length: 320 

내가 무엇입니까 오류 메시지는 다음과 같습니다

Notice: Undefined index: joinColumns in /home/xxx/public_html/products/vendor/doctrine/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1217 

누군가는 왜 작동하지 않습니다에 대한 몇 가지 빛을 할 수있을 것입니다.

답변

8

나는이 오래된 질문이다 알지만, 다른 사람이 (나처럼) 구글을 통해 여기에 도착하면, 나는 findBy을 피하고 저장소에 DQL을 사용했다 :

$products = $em->getRepository('Vendor\Bundle\Entity\Product')->findByColours($colours); 

그리고 저장소에 :

public function findByColours($colours) 
{ 
    $qb = $this->getEntityManager()->createQueryBuilder(); 
    $qb ->select(array('p')) 
     ->from('VendorBundle:Product', 'p') 
     ->join('p.colours', 'c', 'WITH', $qb->expr()->in('c.id', $colours)); 
    $result = $qb->getQuery()->execute(); 
    return $result; 

} 

$ 색상에 따라 결합을 변경해야 할 수도 있습니다. 이것은 색상 ID의 배열이라고 가정합니다. 문자열 인 경우 in()을 무시하거나 문자열 배열 인 경우 문자열을 매개 변수로 바인딩해야합니다 (다음 링크 참조). expr() 등의 설명은 Doctrine docs입니다.

Undefined index: joinColumns이 발생하는지 알 수 없지만이 방법을 모두 단계별로 수행하는 방법입니다. 다행히도 누군가가 오류에 대해 명확히 할 수 있기를 바랍니다. 내 솔루션이 다 대다 관계에 추가 작업을 추가하기 때문입니다.