Skip to content

Commit 63b1911

Browse files
authored
Merge branch 'master' into fix-php-doc-generation
2 parents c0b1399 + 9b417e5 commit 63b1911

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Yii Framework 2 Change Log
2222
- Bug #20489: Replace deprecated `strftime` with `date` in `YiiRequirementChecker` (max-s-lab)
2323
- Bug #20494: Fix `PHPdoc`, add `PHPStan/Psalm` annotations for `authMethods` property in `CompositeAuth` class (terabytesoftw)
2424
- Bug #20485: Fix error `Cannot unset string offsets` in `yii\di\Instance:ensure(['__class' => ...], 'some\class\name')` (max-s-lab)
25+
- Enh #20505: `ArrayDataProvider` key handling with flexible path support (fetus-hina)
2526

2627
2.0.53 June 27, 2025
2728
--------------------

framework/data/ArrayDataProvider.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@
5353
class ArrayDataProvider extends BaseDataProvider
5454
{
5555
/**
56-
* @var string|callable|null the column that is used as the key of the data models.
57-
* This can be either a column name, or a callable that returns the key value of a given data model.
56+
* @var string|array|callable|null the column that is used as the key of the data models.
57+
* This can be either a column name, a dot-separated path, an array of keys, or a callable
58+
* that returns the key value of a given data model.
5859
* If this is not set, the index of the [[models]] array will be used.
5960
* @see getKeys()
6061
*/
@@ -103,16 +104,7 @@ protected function prepareModels()
103104
protected function prepareKeys($models)
104105
{
105106
if ($this->key !== null) {
106-
$keys = [];
107-
foreach ($models as $model) {
108-
if (is_string($this->key)) {
109-
$keys[] = $model[$this->key];
110-
} else {
111-
$keys[] = call_user_func($this->key, $model);
112-
}
113-
}
114-
115-
return $keys;
107+
return ArrayHelper::getColumn($models, $this->key, false);
116108
}
117109

118110
return array_keys($models);

tests/framework/data/ArrayDataProviderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,25 @@ public function testGetKeys()
183183
];
184184
$dataProvider = new ArrayDataProvider(['allModels' => $mixedArray, 'pagination' => $pagination]);
185185
$this->assertEquals(['key1', 9], $dataProvider->getKeys());
186+
187+
$nestedArray = [
188+
['foo' => ['bar' => 'key1']],
189+
['foo' => ['bar' => 'key2']],
190+
['foo' => ['bar' => 'key3']],
191+
];
192+
$dataProvider = new ArrayDataProvider([
193+
'allModels' => $nestedArray,
194+
'key' => ['foo', 'bar'],
195+
'pagination' => $pagination,
196+
]);
197+
$this->assertEquals(['key1', 'key2'], $dataProvider->getKeys());
198+
199+
$dataProvider = new ArrayDataProvider([
200+
'allModels' => $nestedArray,
201+
'key' => 'foo.bar',
202+
'pagination' => $pagination,
203+
]);
204+
$this->assertEquals(['key1', 'key2'], $dataProvider->getKeys());
186205
}
187206

188207
public function testSortFlags()

0 commit comments

Comments
 (0)