在 Symfony 2 中急切加载相关实体 [英] Eager loading of related entity in Symfony 2

查看:20
本文介绍了在 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 实体的消息时要懒惰.所以 $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.

但我也为 Message 实体的附件定义了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:

客户.php

class Customer
{
    /**
     * @ORMOneToMany(targetEntity="Message", mappedBy="customer")
     * @ORMOrderBy({"createdOn" = "DESC"})
     */
    private $messages;

Message.php

Message.php

class Message
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity="Customer", inversedBy="messages")
     * @ORMJoinColumn(name="customer_id", referencedColumnName="id")
     **/
    private $customer;
    /**
     * @ORMOneToMany(targetEntity="Attachment", mappedBy="message", fetch="EAGER")
     **/
    private $attachments;

附件.php:

class Attachment
{

    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity="Message", inversedBy="attachments")
     * @ORMJoinColumn(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://学说-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天全站免登陆