getArrayResult on entity with ManyToOne associatio

2019-02-21 21:15发布

Having the follow basic tables (one-to-many relationship)
Client - Has many users.
Users - Each user belongs to single client.

In a very simple example if I query the user entity (Querybuilder) with getArrayResult() I see the following:

  1. The actual generated SQL contains the foreign key field to be returned (i.e. ClientID)
  2. The actual returned data array does NOT contain the foreign key field.

At this stage I do not need to return foreign data and so do not need to join to the associated table.

So question is...
What or how do I return the foreign key value in my array?

Query is:

   $qb = $this->_em->createQueryBuilder();  
   $qb->select('e');  
   $qb->from('Entity\User', 'e');  

SQL is:

SELECT w0_.Id AS Id0, w0_.Name AS Name2, w0_.ClientID AS ClientID7
FROM users w0_  

2条回答
Luminary・发光体
2楼-- · 2019-02-21 21:56

Try to set the HINT_INCLUDE_META_COLUMNS query hint on the query (not the builder) before you execute it.

$q->setHint(Query::HINT_INCLUDE_META_COLUMNS, true);
查看更多
叛逆
3楼-- · 2019-02-21 21:57

As far as I know, you can't do this, because ClientID is not a property of User. User has a property like $client, and that client entity has an $id.

This is confusing you because you're dealing with Entites but you're still thinking in SQL.

The solution, though slightly less efficient, would probably be to join the Client entity into your DQL query, and then get $results[N]['client']['id'] (or similar, I'm not too familiar with getResultArray())

查看更多
登录 后发表回答