Symfony2:是否获取="EAGER"?创建联接? [英] Symfony2: does fetch="EAGER" create a join?
问题描述
我在product
实体中拥有此映射的属性:
/**
* @ORM\ManyToMany(targetEntity="Group", mappedBy="products", indexBy="id", fetch="EAGER")
*
*/
protected $groups;
我想知道,我对fetch="EAGER"
的理解是,一旦选择了产品,它应该获取组,这是发生了什么,但是每当我执行类似findBy()
的操作时,它都会使用2个查询,一个查询来获取product
另一个获得groups
.
无论如何,是否可以通过一个查询使findBy()
或其他辅助方法将product
连同其groups
一起获得,还是唯一的方法是编写自定义存储库函数并自己执行LEFT-JOIN
? >
更新
我尝试了多种解决方案,最终覆盖了findBy()
函数,例如:
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$q = $this
->createQueryBuilder('u')
->select('u, g')
->leftJoin('u.groups', 'g')
->setFirstResult( $offset )
->setMaxResults( $limit );
foreach ($criteria as $field => $value)
{
$q
->andWhere(sprintf('u.%s = :%s', $field, $field))
->setParameter($field, $value)
;
}
foreach ($orderBy as $field => $value)
{
$q->addOrderBy(sprintf('u.%s',$field),$value);
}
try
{
$q = $q->getQuery();
$users = $q->getResult();
return $users;
}
catch(ORMException $e)
{
return null;
}
}
问题
1-我可以使用fetch="EAGER"
来使findB在一个查询中返回product
及其groups
2-如果不是,那么我有没有将fetch="EAGER"
与多对多实体一起使用而不会降低性能
3-覆盖findBy是一个好方法吗?有什么缺点吗?
谢谢
FETCH_EAGER模式存在问题.实际上,此处有一个公开请求来解决此问题,但还没有还没关门.
我建议使用自定义存储库以所需的方式获取数据.
I have this mapped property inside my product
entity:
/**
* @ORM\ManyToMany(targetEntity="Group", mappedBy="products", indexBy="id", fetch="EAGER")
*
*/
protected $groups;
I wonder, my understanding for fetch="EAGER"
is that it should get the groups once the product is selected, this is what happens but it uses 2 queries whenever i do something like findBy()
one query to get the product
and another one to get the groups
.
Is there anyway to make findBy()
or other helper methods get the product
along with its groups
in one query or the only way is to write a custom repository function and do a LEFT-JOIN
myself?
UPDATE
I tried multiple solutions and endup overwriting the findBy()
function something like:
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$q = $this
->createQueryBuilder('u')
->select('u, g')
->leftJoin('u.groups', 'g')
->setFirstResult( $offset )
->setMaxResults( $limit );
foreach ($criteria as $field => $value)
{
$q
->andWhere(sprintf('u.%s = :%s', $field, $field))
->setParameter($field, $value)
;
}
foreach ($orderBy as $field => $value)
{
$q->addOrderBy(sprintf('u.%s',$field),$value);
}
try
{
$q = $q->getQuery();
$users = $q->getResult();
return $users;
}
catch(ORMException $e)
{
return null;
}
}
Questions
1- Can i use fetch="EAGER"
to make findBy return the product
along with its groups
in one query
2- If not then is there any case that i use fetch="EAGER"
with many-to-many entities without killing performance
3- Is overriding the findBy is a good approach? any drawbacks?
Thanks,
The FETCH_EAGER mode has it's problems. In fact there is an open request here to resolve this issue but hasn't been closed yet.
I recommend using a custom repository to fetch the data in the manner you want it.
这篇关于Symfony2:是否获取="EAGER"?创建联接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!