Symfony 2中相关实体的提前加载 [英] Eager loading of related entity in Symfony 2

查看:53
本文介绍了Symfony 2中相关实体的提前加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

共有三个实体:客户,消息,附件。

There are three entities: Customer, Messages, Attachments.

这些实体之间的关系很简单:一个客户可以有很多消息,一条消息可以有很多附件。两种关系都是一对多的。

The relationship between these entities is straight forward: A customer can have many messages and a message can have many attachments. Both relations are "one-to-many".

我告诉该学说在为客户实体加载消息时要保持懒惰。因此, $ customer-> getMessages()会导致附加的SQL语句。没关系。

I told doctrine to be lazy when loading the messages for the Customer entity. So $customer->getMessages() results in an additional SQL statement. That's fine.

但是我还为消息实体的附件定义了 EAGER加载。

But I also defined an "EAGER" loading for the attachments for the Message entity.

现在,我希望通过调用 $ customer-> getMessages()得到的消息已经加载了所有附件。但是 $ message-> getAttachments()仍然会导致每条消息包含一条SQL语句。

Now I would have expected that the messages I get by calling $customer->getMessages() are already loaded with all their attachments. But $message->getAttachments() still causes one SQL statement per message.

这是预期的行为吗?

仅供参考,但我的课程除外:

Just for reference, excepts from my classes:

Customer.php

Customer.php

class Customer
{
    /**
     * @ORM\OneToMany(targetEntity="Message", mappedBy="customer")
     * @ORM\OrderBy({"createdOn" = "DESC"})
     */
    private $messages;

Message.php

Message.php

class Message
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Customer", inversedBy="messages")
     * @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
     **/
    private $customer;
    /**
     * @ORM\OneToMany(targetEntity="Attachment", mappedBy="message", fetch="EAGER")
     **/
    private $attachments;

Attachment.php:

Attachment.php:

class Attachment
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Message", inversedBy="attachments")
     * @ORM\JoinColumn(name="message_id", referencedColumnName="id")
     **/
    private $message;


推荐答案

这听起来像是我的预期行为。该学说的文档似乎暗示着渴望获取仅是一个层次。

It sounds like expected behavior to me. The doctrine documentation seems to imply that eager fetching is only one level deep.

根据文档:


每当您查询具有持久关联的实体,并且将这些关联的
映射为EAGER时,它们将自动与被查询的实体一起加载
并因此立即被
可用于您的应用程序。

Whenever you query for an entity that has persistent associations and these associations are mapped as EAGER, they will automatically be loaded together with the entity being queried and is thus immediately available to your application.

http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html#by -eager-loading

在您的案例中要查询的实体是客户,而客户渴望消息,因此消息被填充。但是,消息不是要查询的对象,因此不会加载附件。

The entity being queried in your case is customer and customer has eager on messages so messages are populated. Messages, however are not the object being queried, so attachments do not get loaded.

这篇关于Symfony 2中相关实体的提前加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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