Symfony2 和 Doctrine:优化查询(获取 EAGER)
Symfony2 & Doctrine: Optimizing Query (Fetching EAGER)
目前我的查询是这样的:
$customers = $em->createQueryBuilder()
->select('c')
->from('AppBundle:Customer', 'c')
->orderBy('c.id', 'ASC')
->setFirstResult($offset)
->setMaxResults($max)
->getQuery()
->getResult();
这 returns 我是客户实体的所有数据。但是,在我的 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();
问题
此查询的问题在于它 returns 电话号码本身。这意味着在 $customers 中我没有带有 PhoneNumbers 的 Customers。相反,$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"
}
这几乎是正确的。我会改变:
- 删除一个额外的
addSelect()
- 将所有内容放在一个 select()
中
- 更改
leftJoin
以利用 @ManyToMany
关系
所以,像这样:
$customers = $em->createQueryBuilder()
->select('c', 'ph')
->from('AppBundle:Customer', 'c')
->leftJoin('c.phoneNumbers', 'ph')
->orderBy('c.id', 'ASC')
->setFirstResult($offset)
->setMaxResults($max)
->getQuery()
->getResult();
希望这对您有所帮助...
目前我的查询是这样的:
$customers = $em->createQueryBuilder()
->select('c')
->from('AppBundle:Customer', 'c')
->orderBy('c.id', 'ASC')
->setFirstResult($offset)
->setMaxResults($max)
->getQuery()
->getResult();
这 returns 我是客户实体的所有数据。但是,在我的 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();
问题 此查询的问题在于它 returns 电话号码本身。这意味着在 $customers 中我没有带有 PhoneNumbers 的 Customers。相反,$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"
}
这几乎是正确的。我会改变:
- 删除一个额外的
addSelect()
- 将所有内容放在一个select()
中
- 更改
leftJoin
以利用@ManyToMany
关系
所以,像这样:
$customers = $em->createQueryBuilder()
->select('c', 'ph')
->from('AppBundle:Customer', 'c')
->leftJoin('c.phoneNumbers', 'ph')
->orderBy('c.id', 'ASC')
->setFirstResult($offset)
->setMaxResults($max)
->getQuery()
->getResult();
希望这对您有所帮助...