Doctrine adds calculated fields

Now I have a profile object that has a 1: 1 relationship to the address object.

if I create the following simple query

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

It returns some data as follows:

firstname: Test
lastname: Bla
gender: male
dateOfBirth: '1972-03-14'
address:
    city: Brussel
    country: BE

This is exactly what I want, but now I want to add an age field. It works when I do it like this:

$this->qb = $this->_em->createQueryBuilder()
    ->select('e','YEAR(CURRENT_DATE()) - YEAR(e.dateOfBirth) AS age')
    ->from($this->_entityName, 'e')
;

But the result ends:

0:
    firstname: Test
    lastname: Bla
    gender: male
    dateOfBirth: '1972-03-14'
    address:
        city: Brussel
        country: BE
age: '42'

Thus, it puts age in a different result and puts all the data from the object into key 0. Now, how can I do this so that age simply becomes part of the entity data?

Here is how I would like:

firstname: Test
lastname: Bla
gender: male
dateOfBirth: '1972-03-14'
age: 36
address:
    city: Brussel
    country: BE

And also the result is currently similar to this because of the JMSSerializer, in an object that just doesnโ€™t serialize, there are more fields, but they are still extracted from the database ... Is there a way to get the doctrine to get only the fields you need, keeping the same result?

+4
1

Profile, :

public function getAge()
{
    // calculate age here

    $age = ..... ;

    return $age;
}
+1

All Articles