如何在不加载关联对象的情况下在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实体具有一个与User实体相关联的字段createdBy。
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。如果我直接在发布查询中加入用户,则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(\Doctrine\ORM\Query::HINT_INCLUDE_META_COLUMNS, true);
$q->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
水化被禁用,因此您将结果作为数组。
Hydratation is disabled, so you have your result as an array.
这篇关于如何在不加载关联对象的情况下在Doctrine 2中获取关联外键ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!