简体   繁体   English

如何在Doctrine 2中获取没有关联映射的实体

[英]How to fetch entities without their association mapping in Doctrine 2

If you have a set of association mapped entities in Doctrine, sometimes you might want to retrieve those entities without its mapped associations being fetched and slowing the query down. 如果在Doctrine中有一组关联映射实体,有时您可能希望检索这些实体而不提取其映射关联并减慢查询速度。

For example I have a set of entities which are association mapped in a chain of linked database tables. 例如,我有一组实体,它们是链接数据库表链中的关联映射。 They are all OnetoMany associations and act as a hierarchy of prices in matrices on product pages. 它们都是OnetoMany协会,并作为产品页面矩阵中价格的层次结构。 They can be represented as so: 它们可以表示为:

SitePage->SiteMatrix->SiteItems->SiteItemPrices.

The associated mapping works perfectly, and when I use the findBy method to get the root SitePage object it contains arrays which represent the mapped entities down the chain. 关联映射工作正常,当我使用findBy方法获取根SitePage对象时,它包含表示链中映射实体的数组。 In other words the SitePage object contains all matrices, which contains all items which contains all prices. 换句话说,SitePage对象包含所有矩阵,其中包含包含所有价格的所有项目。 So far so good. 到现在为止还挺好。

My problem is that every time I get a list of pages on my site, doctrine is walking the entire association mapping tree and returning me with the entire datatabase which is very slow. 我的问题是,每当我在我的网站上获得一个页面列表时,doctrine就会遍历整个关联映射树并返回整个数据库,这非常慢。 Sometime I want to just get my SitePage entity by ID and not contain all the mapped associations. 有时我想通过ID获取我的SitePage实体,而不包含所有映射的关联。

I have looked into lazy and extra lazy loading of associations but they only seem to affect certain functions, and not findBy etc. The official documentation is far from helpful: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/tutorials/extra-lazy-associations.html 我已经研究了懒惰和额外的延迟加载关联,但它们似乎只影响某些函数,而不是findBy等。官方文档远非有用: http ://doctrine-orm.readthedocs.org/projects/doctrine-orm /en/latest/tutorials/extra-lazy-associations.html

Other similar questions on Stack Overflow have gone unanswered: Doctrine 2 Association Mapping Overhead? 关于Stack Overflow的其他类似问题没有得到答复: Doctrine 2 Association Mapping Overhead?

Is there an easy way to fetch an entity without its mapped associations? 是否有一种简单的方法来获取没有映射关联的实体? The easiest way I can see currently is to create two entities for each database table, one with association mapping and one without for use in the separate situations where they are required. 我目前看到的最简单的方法是为每个数据库表创建两个实体,一个具有关联映射,另一个不需要在需要它们的单独情况下使用。 It seems odd to me that you cannot simply fetch an entity and specify whether you want to link to it to other entities or fetch it by itself. 对我来说,你不能简单地获取一个实体并指定是否要将其链接到其他实体或自己获取它,这似乎很奇怪。

Thanks for any information on the subject. 感谢您提供有关该主题的任何信息。

The JMSSerializer exclusion strategies can help you. JMSSerializer排除策略可以帮助您。

First, exclude all properties by default : 首先,默认情况下排除所有属性:

// ...

use JMS\Serializer\Annotation as JMS;

/**
 * @ORM\Entity
 * @JMS\ExclusionPolicy("all")
 */
class Foo

Then, choose to exclude or expose your properties : 然后,选择排除或公开您的属性:

/**
 * @ORM\Column(type="string")
 * @JMS\Expose
 */
protected $name;

Also, for your associations, you can use the MaxDepth to restrict the associated entries of your results. 此外,对于您的关联,您可以使用MaxDepth来限制结果的关联条目。 Example : 示例:

// Entity
/** @MaxDepth(1) */
private $selfAssociatedEntries;

// Controller
use JMS\Serializer\SerializationContext;

$serializer->serialize($data, 'json', SerializationContext::create()->enableMaxDepthChecks());

Like this, your entity will contains the $selfAssociatedEntries serialised, but the $selfAssociatedEntries doesn't have the $selfAssociationEntries property. 像这样,您的实体将包含序列化的$selfAssociatedEntries ,但$selfAssociatedEntries没有$selfAssociationEntries属性。
The serialisation is restricted to the first parent object. 序列化仅限于第一个父对象。

Groups are a powerful feature that allows you to expose properties for some actions and exclude them for another : Groups是一个强大的功能,允许您公开某些操作的属性并将其排除在另一个操作之外:

/**
 * @ORM\Column(type="string", length=255, nullable=true, unique=false)
 * @JMS\Groups({"default"}) // Set the group to expose the property
 */
protected $name;

In your controller, set the group used for serialisation : 在控制器中,设置用于序列化的组:

// Property 'name' is exposed
$serializer->serialize($data, 'json', SerializationContext::create()->setGroups(array('default')));

For more informations, look at Exclusion Strategies chapter of the documentation. 有关更多信息,请查看文档的“ 排除策略”一章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM