Skip to content

Commit

Permalink
Rename option "idField" to "keyField".
Browse files Browse the repository at this point in the history
The latter is more intuitive.
  • Loading branch information
ADmad committed Feb 19, 2015
1 parent 67d073e commit f64b556
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
32 changes: 23 additions & 9 deletions src/ORM/Table.php
Expand Up @@ -908,7 +908,7 @@ public function findAll(Query $query, array $options)
*
* ```
* $table->find('list', [
* 'idField' => 'name',
* 'keyField' => 'name',
* 'valueField' => 'age'
* ]);
* ```
Expand Down Expand Up @@ -943,18 +943,25 @@ public function findAll(Query $query, array $options)
public function findList(Query $query, array $options)
{
$options += [
'idField' => $this->primaryKey(),
'keyField' => $this->primaryKey(),
'valueField' => $this->displayField(),
'groupField' => null
];

if (isset($options['idField'])) {
$options['keyField'] = $options['idField'];
unset($options['idField']);
trigger_error('Option "idField" is deprecated, use "keyField" instead.', E_USER_WARNING);
}

$options = $this->_setFieldMatchers(
$options,
['idField', 'valueField', 'groupField']
['keyField', 'valueField', 'groupField']
);

return $query->formatResults(function ($results) use ($options) {
return $results->combine(
$options['idField'],
$options['keyField'],
$options['valueField'],
$options['groupField']
);
Expand All @@ -970,12 +977,12 @@ public function findList(Query $query, array $options)
*
* You can customize what fields are used for nesting results, by default the
* primary key and the `parent_id` fields are used. If you wish to change
* these defaults you need to provide the keys `idField` or `parentField` in
* these defaults you need to provide the keys `keyField` or `parentField` in
* `$options`:
*
* ```
* $table->find('threaded', [
* 'idField' => 'id',
* 'keyField' => 'id',
* 'parentField' => 'ancestor_id'
* ]);
* ```
Expand All @@ -987,13 +994,20 @@ public function findList(Query $query, array $options)
public function findThreaded(Query $query, array $options)
{
$options += [
'idField' => $this->primaryKey(),
'keyField' => $this->primaryKey(),
'parentField' => 'parent_id',
];
$options = $this->_setFieldMatchers($options, ['idField', 'parentField']);

if (isset($options['idField'])) {
$options['keyField'] = $options['idField'];
unset($options['idField']);
trigger_error('Option "idField" is deprecated, use "keyField" instead.', E_USER_WARNING);
}

$options = $this->_setFieldMatchers($options, ['keyField', 'parentField']);

return $query->formatResults(function ($results) use ($options) {
return $results->nest($options['idField'], $options['parentField']);
return $results->nest($options['keyField'], $options['parentField']);
});
}

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
Expand Up @@ -406,7 +406,7 @@ public function testFindTranslationsList()
$table->addBehavior('Translate', ['fields' => ['title', 'body']]);
$results = $table
->find('list', [
'idField' => 'title',
'keyField' => 'title',
'valueField' => '_translations.deu.title',
'groupField' => 'id'
])
Expand Down

0 comments on commit f64b556

Please sign in to comment.