I am trying to create a query with doctrine, which basically should look like that in SQL:
SELECT p.* FROM some_tabel AS p GROUP BY p.type;
private function getAll()
//$this->em => Entity Manager
SELECT p.*, max(p.id) FROM some_tabel AS p GROUP BY p.type;
private function groupByType(array $robots)
$list[$robot->getType()] = $robot;
You can't group by a single column, and select other. MySQL has this "bug", that allow users to group by one column while select many columns.
In other words, you have to
group by all columns that are not using aggregate functions, like SUM, AVG, MAX, COUNT.
The side-effect of this "feature" of MySQL is that will return the first result for other columns, even if it doesn't represent the truth.
Try this script:
create table test(id int, name char(1), value1 int); insert into test values(1, 'a', 1); insert into test values(2, 'b', 1); select name, value1, max(id) from test group by value1;
name: 'b', value1: 1, max(id): 2
name: 'a', value1: 1, max(id): 2