Symfony2:是否获取="EAGER"?创建联接? [英] Symfony2: does fetch="EAGER" create a join?

查看:88
本文介绍了Symfony2:是否获取="EAGER"?创建联接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆