Skip to content

Commit

Permalink
Account for 'groupField' when auto selecting fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Apr 27, 2015
1 parent ec8f1a1 commit 5c910f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/ORM/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,11 @@ public function findList(Query $query, array $options)
}

if (!$query->clause('select')) {
$fields = array_merge((array)$options['keyField'], (array)$options['valueField']);
$fields = array_merge(
(array)$options['keyField'],
(array)$options['valueField'],
(array)$options['groupField']
);
$columns = $this->schema()->columns();
if (count($fields) === count(array_intersect($fields, $columns))) {
$query->select($fields);
Expand Down
20 changes: 12 additions & 8 deletions tests/TestCase/ORM/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1197,19 +1197,21 @@ public function testFindListSelectedFields()
$expected = ['id', 'username'];
$this->assertSame($expected, $query->clause('select'));

$select = ['odd' => new QueryExpression('id % 2')];
$query = $table->find('list', ['groupField' => 'odd'])
->select($select)
->order('id');
$expected = array_merge(['id', 'username'], $select);
$this->assertSame($expected, $query->clause('select'));

$expected = ['odd' => new QueryExpression('id % 2'), 'id', 'username'];
$query = $table->find('list', [
'fields' => $expected,
'groupField' => 'odd',
]);
$this->assertSame($expected, $query->clause('select'));

$articles = new Table([
'table' => 'articles',
'connection' => $this->connection,
]);

$query = $articles->find('list', ['groupField' => 'author_id']);
$expected = ['id', 'title', 'author_id'];
$this->assertSame($expected, $query->clause('select'));
}

/**
Expand All @@ -1229,7 +1231,6 @@ public function testFindListWithVirtualField()
$query = $table
->find('list')
->order('id');

$this->assertEmpty($query->clause('select'));

$expected = [
Expand All @@ -1239,6 +1240,9 @@ public function testFindListWithVirtualField()
4 => 'bonus'
];
$this->assertSame($expected, $query->toArray());

$query = $table->find('list', ['groupField' => 'odd']);
$this->assertEmpty($query->clause('select'));
}

/**
Expand Down

0 comments on commit 5c910f0

Please sign in to comment.