你可以从Doctine2中的对象获取一个外键,而不加载该对象吗? [英] Can you get a foreign key from an object in Doctine2 without loading that object?

查看:136
本文介绍了你可以从Doctine2中的对象获取一个外键,而不加载该对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个活动网站上工作,并且在生产和表演之间有一对多的关系,当我有一个表演对象,如果我需要它的生产id,我必须做的时候

I'm working on an events site and have a one to many relationship between a production and its performances, when I have a performance object if I need its production id at the moment I have to do

$productionId = $performance->getProduction()->getId();

如果我只是需要生产id,似乎是浪费发送另一个数据库查询获取已经在对象某处的值。
有没有办法?

In cases when I literally just need the production id it seems like a waste to send off another database query to get a value that's already in the object somewhere. Is there a way round this?

推荐答案

编辑2013.02.17:

什么下面写的不再是真的。 您不需要在问题中概述的场景中执行任何操作,因为Doctrine足够聪明地将id字段加载到相关实体中,因此代理对象将已经包含id,并且它将不发出另一个数据库调用。

Edit 2013.02.17:
What I wrote below is no longer true. You don't have to do anything in the scenario outlined in the question, because Doctrine is clever enough to load the id fields into related entities, so the proxy objects will already contain the id, and it will not issue another call to the database.

下面的过期答案:

这是可能的,但它是未经修改的

It is possible, but it is unadvised.

原因在于,Doctrine试图真正遵守您的实体应该形成对象图的原则,外键在哪里没有地方,因为它们是

The reason behind that, is Doctrine tries to truly adhere to the principle that your entities should form an object graph, where the foreign keys have no place, because they are just "artifacts", that come from the way relational databases work.

您应该将关联重写为


  • 加载,如果您始终需要相关实体

  • 编写DQL查询(最好在存储库中)来获取加入相关实体

  • 让它通过调用getter来懒惰加载相关实体

如果你不信服,一个nd真的想避免以上所有的,有两种方式(我知道),获取一个相关对象的id,而不会触发负载,而不用诉诸反射和序列化的技巧:

If you are not convinced, and really want to avoid all of the above, there are two ways (that I know of), to get the id of a related object, without triggering a load, and without resorting to tricks like reflection and serialization:

如果您已经拥有对象,您可以检索内部使用的内部 UnitOfWork 对象,并使用它 getEntityIdentifier()方法,将其传递给卸载的实体代理对象)。它将返回您的ID,而不会触发惰性负载。

If you already have the object in hand, you can retrieve the inner UnitOfWork object that Doctrine uses internally, and use it's getEntityIdentifier() method, passing it the unloaded entity (the proxy object). It will return you the id, without triggering the lazy-load.

假设您具有多对一的关系,多个属于一个类别的文章:

Assuming you have many-to-one relation, with multiple articles belonging to a category:

$articleId = 1;
$article = $em->find('Article', $articleId);
$categoryId = $em->getUnitOfWork()->getEntityIdentifier($article->getCategory());

即将到来,您将可以使用IDENTITY DQL函数来仅选择一个外键,像这样:

Coming 2.2, you will be able to use the IDENTITY DQL function, to select just a foreign key, like this:

SELECT IDENTITY(u.Group) AS group_id FROM User u WHERE u.id = ?0

已经承诺到开发版本。

仍然,您应该真的试图坚持一种正确的方法。

Still, you should really try to stick to one of the "correct" methods.

这篇关于你可以从Doctine2中的对象获取一个外键,而不加载该对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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