GetArrayResult for an entity with ManyToOne association

Availability of the following main tables (one-to-many relationship) Client. Has many users.
Users - each user belongs to one client.

In a very simple example, if I query a custom object (Querybuilder) with getArrayResult() I see the following:

  • The actually generated SQL contains the foreign key field that should be (i.e. ClientID)
  • The actual returned dataset does NOT contain a foreign key field.

At this stage, I do not need to return foreign data, and therefore do not need to join the linked table.

So the question is ... What and how to return the value of a foreign key in my array?

Request:

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

SQL:

 SELECT w0_.Id AS Id0, w0_.Name AS Name2, w0_.ClientID AS ClientID7 FROM users w0_ 
+8
php orm doctrine2
source share
2 answers

Try to set the query hint HINT_INCLUDE_META_COLUMNS request (not the creator) before executing it.

 $q->setHint(Query::HINT_INCLUDE_META_COLUMNS, true); 
+16
source share

As far as I know, you cannot do this because ClientID is not a User property. The user has a property, such as $ client, and this client object has $ id.

It bothers you because you are dealing with Entites, but you are still thinking in SQL.

The solution, although a little less efficient, would probably be to associate the client object with your DQL query, and then get $results[N]['client']['id'] (or the like, I'm not too familiar with getResultArray ())

0
source share

All Articles