你能从 Doctrine2 中的对象获取外键而不加载那个对象吗? [英] Can you get a foreign key from an object in Doctrine2 without loading that object?

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

问题描述

我正在一个活动网站上工作,并且制作与其表演之间存在一对多的关系,当我有表演对象时,如果我需要它的制作 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?

推荐答案

Edit 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 来延迟加载相关实体

如果你不相信,并且真的想避免上述所有情况,有两种方法(我知道),获取相关对象的 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:

如果您已经拥有该对象,您可以检索 Doctrine 内部使用的内部 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());

在 2.2 中,您将能够使用 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.

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

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