doctrine2加载使用太多SQL查询的提取模式渴望的一对多关联 [英] doctrine2 loads one-to-many associations with fetch mode eager using too many SQL queries

查看:110
本文介绍了doctrine2加载使用太多SQL查询的提取模式渴望的一对多关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载许多实体的列表。

这些实体与其他实体有一对多关联。

我想将所有这些其他实体加载到一个单个SQL查询(而不是第一个列表中每个实体的一个查询)。

I am loading a list of many entities.
These entities have a one-to-many association to other entities.
I want to load all these other entities in one single SQL query (instead of one query for every entity in the first list).

如doctrine2文档所述: http:// www。 doctrine-project.org/docs/orm/2.1/en/reference/dql-doctrine-query-language.html#temporarily-change-fetch-mode-in-dql 这可以用EAGER加载

As discribed in the doctrine2 documentation: http://www.doctrine-project.org/docs/orm/2.1/en/reference/dql-doctrine-query-language.html#temporarily-change-fetch-mode-in-dql this should be possible with "EAGER" loading.

但它不能像描述那样工作。

but it does not work as described.

我的代码:

class User{
    /**
     * @ORM\OneToMany(targetEntity="Address", mappedBy="user", indexBy="id", fetch="EAGER")
     */
    protected $addresses;
    public function __construct(){
        $this->addresses = new ArrayCollection();
    }
}

class Address{
    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="addresses")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="UserId", referencedColumnName="id")
     * })
     */
    private $user;
}

class UserRepository{
    public function findUsersWithAddresses(){
        return $this->getEntityManager()
            ->createQuery('SELECT u FROM MyBundle:User u ORDER BY u.name ASC')
            ->setFetchMode('MyBundle\Entity\User', 'addresses', \Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER)
            ->setMaxResults(10)
            ->getResult();
    }
}

方法UserRepository :: findUsersWithAddresses()执行11 SQL查询。

The method UserRepository::findUsersWithAddresses() executes 11 SQL Queries.

如何告诉Doctrine只使用一个SQL查询来加载地址实体?

我正在使用:


  • symfony v2.0.9

  • doctrine-common 2.1.4

  • doctrine-dbal 2.1.5

  • doctrine 2.1.5

  • symfony v2.0.9
  • doctrine-common 2.1.4
  • doctrine-dbal 2.1.5
  • doctrine 2.1.5

推荐答案

目前版本的原则不支持这一点。

The current version of doctrine doesn't support this.

有一个功能请求关于这个在doctrine2问题跟踪器。

There is a feature request about this in the doctrine2 issue tracker.

所以我希望它将很快实施。

So I hope it will be implemented soon.

这篇关于doctrine2加载使用太多SQL查询的提取模式渴望的一对多关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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