如何在不加载关联对象的情况下获取 Doctrine 2 中的关联外键 ID? [英] How to get association foreign key IDs in Doctrine 2 without loading the associated object?
问题描述
您好,我在我认为很容易的事情上遇到了麻烦.
Hi I'm having trouble with what I thought would be an easy task.
我正在从数据库中检索帖子.Post 实体有一个字段 createdBy,该字段与 User 实体相关联.
I am retrieving a post from the database. The Post entity has a field createdBy which is associated to a User entity.
我想做的是用两个单独的查询(无连接)加载 Post 和 User.这意味着我需要访问 $post 对象上的 created_by 外键整数.教义似乎根本没有暴露这一点.帖子的 var_dump 显示 createdBy => null.如果我直接在 post 查询 createdBy => User 对象中加入用户.有没有办法从帖子中获取 created_by 外键整数,以便我可以查询用户?
What I would like to do is load Post and User with two separate queries (no join). That means I need to have access to the created_by foreign key integer on the $post object. Doctrine does not seem to expose that at all. A var_dump of post shows createdBy => null. If I join the user on directly in the post query createdBy => User object. Is there no way to get the created_by foreign key integer from post so I can query for the user?
谢谢
推荐答案
在您的查询中使用:
$q->setHint(DoctrineORMQuery::HINT_INCLUDE_META_COLUMNS, true);
$q->getResult(DoctrineORMQuery::HYDRATE_ARRAY);
水合被禁用,所以你的结果是一个数组.
Hydratation is disabled, so you have your result as an array.
这篇关于如何在不加载关联对象的情况下获取 Doctrine 2 中的关联外键 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!