一个分支机构可能有多个客户,一个客户可能与多个分支机构相关。所以这是一个多对多的关系。
分支:
<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();
所以我的想法是使用 IN 语句。但这是行不通的。
错误:
Fatal error: Object of class DateTime could not be converted to string ..Query\Expr\Func.php on line 48
有什么想法可以正确地做到这一点吗?
最佳答案
尝试在查询中添加联接:
$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();
关于symfony - 在学说中使用 SQL IN 语句过滤查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12968775/