2012-10-19 4 views
2

하나의 지점에 많은 고객이있을 수 있으며 고객이 여러 지점과 관련 될 수 있습니다. 그래서 이것은 다분히 많은 관계입니다.doctrine에서 SQL IN 문을 사용하여 필터 쿼리

지점 :

<many-to-many target-entity="Customer" inversed-by="branches" field="customers"/> 

고객 : 고객의 지점이 지정된 지점 개체를 일치하는 모든 고객을 선택

<many-to-many field="branches" target-entity="Branch" mapped-by="customers"/> 

가 지금은 쿼리 다음을 수행하고자합니다.

$branch = $em->getRepository('MyBundle:Branch') 
       ->findOneById($bid); 

    $qb->select(array('c')) 
    ->from('MyBundle:Customer', 'c') 
    ->where($qb->expr()->in('c.branches', $branch)) 
    ->andWhere('c.delted = 0') 
    ->getQuery(); 

그래서 내 생각은 성명에서 사용하는 것이었다 :

이 내가 시도하는 것이다. 그러나 이것은 효과가 없습니다.

오류 :

Fatal error: Object of class DateTime could not be converted to string ..Query\Expr\Func.php on line 48

모든 아이디어를 어떻게 올바른 방법으로 수행하는 방법?

+0

당신은 단지 개체 사용하여, 그것을 달성 할 수 있어야한다 : $ branch-> getCustomers를(); Doctrine은 나머지 부분에 신경을 쓸 것입니다. (추가 조건을 추가하고 싶지 않으면?) 아니면 내가 놓친 것일까 요? – Cyprian

+0

나도 알아,하지만 쿼리가 아니라 개체의 집합을 기대하는 쪽 매김기에 쿼리를 전달합니다. –

답변

2

쿼리에 조인 추가 할 시도해보십시오

$qb->select(array('c', 'b')) 
    ->from('MyBundle:Customer', 'c') 
    ->join('c.branches', 'b') 
    ->where('b IN (:branch)') 
    ->andWhere('c.deleted = 0') 
    ->setParameter('branch', array($branch)) 
    ->getQuery(); 
+0

+1 매력처럼 작동합니다! 고맙습니다! –