I have this table structure:
CREATE TABLE `inventory_item` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`articleID` int(11) NOT NULL,
`quantity` int(11) NOT NULL,
`date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
And I have this query:
$rows = $this->getModelManager()->createQueryBuilder()
->select('ii')
->from(InventoryItem::class, 'ii')
->where('ii.articleId IN (:articleIds)')
->andWhere('ii.quantity > 0')
->orderBy('ii.date', 'ASC')
->setParameter('articleIds', $articleIds )
->getQuery()
->getResult();
In the Database I can have entities which look like this:
ID | ArticleID | Quantity | Date
1 | 100 | 10 | 2018-08-31
2 | 200 | 20 | 2018-07-31
3 | 100 | 40 | 2018-05-31
Now, when $articleIds in the query are 100, 200 I want to have this output:
ID | ArticleID | Quantity | Date
2 | 200 | 20 | 2018-07-31
3 | 100 | 40 | 2018-05-31
So, when ArticleID is equal the query should only return the entity with the youngest date, but also the entity with ArticleId = 200.
Is there a possibility in the doctrine query builder to achieve this? I tried it with groupBy, but this does not work as the orderBy has no effect on the result when using groupBy.
Thanks!