目前我的查询如下:
$customers = $em->createQueryBuilder()
->select('c')
->from('AppBundle:Customer', 'c')
->orderBy('c.id', 'ASC')
->setFirstResult($offset)
->setMaxResults($max)
->getQuery()
->getResult();
这将返回Customer实体的所有数据。但是,在我的Twig模板中,我必须访问Customer实体与PhoneNumber的@ManyToMany关系。这会导致额外的查询。现在我正在尝试优化此查询:
$customers = $em->createQueryBuilder()
->select('c')
->addSelect('ph')
->from('AppBundle:Customer', 'c')
->leftJoin('AppBundle:PhoneNumber', 'ph', 'WITH', 'ph MEMBER OF c.phoneNumbers')
->orderBy('c.id', 'ASC')
->setFirstResult($offset)
->setMaxResults($max)
->getQuery()
->getResult();
问题 此查询的问题在于它自己返回PhoneNumbers。这意味着在$ customers中我没有使用PhoneNumbers的客户。相反,$ customers是一组Customers和PhoneNumbers。在这样的数组上我无法正确迭代。我该如何解决这个问题?
转储提取
object(stdClass)#2171 (12) {
["__CLASS__"]=>
string(32) "Updoon\AppBundle\Entity\Customer"
["phoneNumbers"]=>
array(2) {
[0]=>
string(35) "Updoon\AppBundle\Entity\PhoneNumber"
[1]=>
string(35) "Updoon\AppBundle\Entity\PhoneNumber"
}
["id"]=>
int(34)
}
object(stdClass)#2171 (9) {
["__CLASS__"]=>
string(35) "AppBundle\Entity\PhoneNumber"
["customers"]=>
array(1) {
[0]=>
string(32) "AppBundle\Entity\Customer"
}
["id"]=>
int(95)
["number"]=>
string(11) "51 06 20 20"
}
答案 0 :(得分:1)
这几乎是正确的。我会改变:
Users.customGET("active")
- 将所有内容放在一个addSelect()
select()
以使用leftJoin
关系所以,像这样:
@ManyToMany
希望这会有所帮助......