Skip to content

Commit 5c910f0

Browse files
committed
Account for 'groupField' when auto selecting fields
1 parent ec8f1a1 commit 5c910f0

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/ORM/Table.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,11 @@ public function findList(Query $query, array $options)
969969
}
970970

971971
if (!$query->clause('select')) {
972-
$fields = array_merge((array)$options['keyField'], (array)$options['valueField']);
972+
$fields = array_merge(
973+
(array)$options['keyField'],
974+
(array)$options['valueField'],
975+
(array)$options['groupField']
976+
);
973977
$columns = $this->schema()->columns();
974978
if (count($fields) === count(array_intersect($fields, $columns))) {
975979
$query->select($fields);

tests/TestCase/ORM/TableTest.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,19 +1197,21 @@ public function testFindListSelectedFields()
11971197
$expected = ['id', 'username'];
11981198
$this->assertSame($expected, $query->clause('select'));
11991199

1200-
$select = ['odd' => new QueryExpression('id % 2')];
1201-
$query = $table->find('list', ['groupField' => 'odd'])
1202-
->select($select)
1203-
->order('id');
1204-
$expected = array_merge(['id', 'username'], $select);
1205-
$this->assertSame($expected, $query->clause('select'));
1206-
12071200
$expected = ['odd' => new QueryExpression('id % 2'), 'id', 'username'];
12081201
$query = $table->find('list', [
12091202
'fields' => $expected,
12101203
'groupField' => 'odd',
12111204
]);
12121205
$this->assertSame($expected, $query->clause('select'));
1206+
1207+
$articles = new Table([
1208+
'table' => 'articles',
1209+
'connection' => $this->connection,
1210+
]);
1211+
1212+
$query = $articles->find('list', ['groupField' => 'author_id']);
1213+
$expected = ['id', 'title', 'author_id'];
1214+
$this->assertSame($expected, $query->clause('select'));
12131215
}
12141216

12151217
/**
@@ -1229,7 +1231,6 @@ public function testFindListWithVirtualField()
12291231
$query = $table
12301232
->find('list')
12311233
->order('id');
1232-
12331234
$this->assertEmpty($query->clause('select'));
12341235

12351236
$expected = [
@@ -1239,6 +1240,9 @@ public function testFindListWithVirtualField()
12391240
4 => 'bonus'
12401241
];
12411242
$this->assertSame($expected, $query->toArray());
1243+
1244+
$query = $table->find('list', ['groupField' => 'odd']);
1245+
$this->assertEmpty($query->clause('select'));
12421246
}
12431247

12441248
/**

0 commit comments

Comments
 (0)