0

When I run:

$this->createQueryBuilder('debtor')
->join('debtor.deals', 'deal')
->addSelect('...something... as closed_deals_count')
->getQuery()
->getSingleResult()

It results in the following structure:

array:3 [
    0 => Debtor {...},
    'closed_deals_count' => 0
]

How could I get a single Debtor record with 'closed_deals_count' in it instead of an array?

hasumedic
  • 2,139
  • 12
  • 17
  • you can't push it inside that array, but you can use extra select field in your query for example in orderby and hide it in result. – Paweł Mikołajczuk Feb 24 '16 at 10:59
  • Doctrine 2 does not handle "virtual columns" like what you want out of the box. You can write your own custom hydrator: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#custom-hydration-modes But it might be overkill. I would just add a line of code and merge the data myself. – Cerad Feb 24 '16 at 14:54

1 Answers1

0

I think you're looking for getSingleScalarResult. Something like:

$this->createQueryBuilder('debtor')
->join('debtor.deals', 'deal')
->addSelect('COUNT(deal.id) as closed_deals_count')
->getQuery()
->getSingleScalarResult()
hasumedic
  • 2,139
  • 12
  • 17
  • Thanks for answer. This code return only one number, but I want to get Entity which includes closed_deals_count. – Роман Слободзян Feb 24 '16 at 12:04
  • That's what you are getting in your question right? An array of Debtor entities with closed_deals_count attached. What are you after, exactly? – hasumedic Feb 24 '16 at 12:19
  • He is actually getting an array containing a debtor entity as well as the closed_deals_count. What he wants is a debtor entity with the closed_deals_count as a property. Somethi9ng which will take a bit more code. – Cerad Feb 24 '16 at 14:56