您可以在不加载该对象的情况下从Doctrine2中的对象获取外键吗? [英] Can you get a foreign key from an object in Doctrine2 without loading that object?

查看:60
本文介绍了您可以在不加载该对象的情况下从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?

推荐答案

编辑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.

下面的过时答案:

可能,但不建议这样做

其背后的原因是,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查询(最好在存储库中)以获取-加入相关实体

  • 让它通过在其上调用吸气剂来延迟加载相关实体

如果您不相信,和我真的很想避免上述所有情况,有两种方法(我知道),即获取相关对象的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()方法,将其传递给未加载的实体(代理对象)。

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

已经

It is already committed to the development versions.

不过,您确实应该尝试使用一种正确的方法。

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

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

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