Skip to content

Commit

Permalink
Fixes yiisoft#12828: Fixed handling of nested arrays, objects in `\yi…
Browse files Browse the repository at this point in the history
…i\grid\GridView::guessColumns`
  • Loading branch information
githubjeka authored and samdark committed Oct 27, 2016
1 parent 333c4b6 commit 757b3de
Showing 3 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -65,6 +65,7 @@ Yii Framework 2 Change Log
- Bug #12605: Make 'safe' validator work on write-only properties (arthibald, CeBe)
- Bug #12629: Fixed `yii\widgets\ActiveField::widget()` to call `adjustLabelFor()` for `InputWidget` descendants (coderlex)
- Bug #12649: Fixed consistency of `indexBy` handling for `yii\db\Query::column()` (silverfire)
- Bug #12828: Fixed handling of nested arrays, objects in `\yii\grid\GridView::guessColumns` (githubjeka)
- Enh #384: Added ability to run migration from several locations via `yii\console\controllers\BaseMigrateController::$migrationNamespaces` (klimov-paul)
- Enh #6996: Added `yii\web\MultipartFormDataParser`, which allows proper processing of 'multipart/form-data' encoded non POST requests (klimov-paul)
- Enh #8719: Add support for HTML5 attributes on submitbutton (formaction/formmethod...) for ActiveForm (VirtualRJ)
4 changes: 3 additions & 1 deletion framework/grid/GridView.php
Original file line number Diff line number Diff line change
@@ -569,7 +569,9 @@ protected function guessColumns()
$model = reset($models);
if (is_array($model) || is_object($model)) {
foreach ($model as $name => $value) {
$this->columns[] = (string) $name;
if ($value === null || is_scalar($value) || is_callable([$value, '__toString'])) {
$this->columns[] = (string) $name;
}
}
}
}
62 changes: 62 additions & 0 deletions tests/framework/grid/GridViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php


namespace yiiunit\framework\grid;

use yii\data\ArrayDataProvider;
use yii\grid\DataColumn;
use yii\grid\GridView;

/**
* @author Evgeniy Tkachenko <[email protected]>
* @group grid
*/
class GridViewTest extends \yiiunit\TestCase
{
public function testGuessColumns()
{
$this->mockApplication();
$row = ['id' => 1, 'name' => 'Name1', 'value' => 'Value1', 'description' => 'Description1',];

$grid = new GridView([
'dataProvider' => new ArrayDataProvider(
[
'allModels' => [
$row,
],
]
),
]);

$columns = $grid->columns;
$this->assertCount(count($row), $columns);

foreach ($columns as $index => $column) {
$this->assertInstanceOf(DataColumn::className(), $column);
$this->assertArrayHasKey($column->attribute, $row);
}

$row = array_merge($row, ['relation' => ['id' => 1, 'name' => 'RelationName',],]);
$row = array_merge($row, ['otherRelation' => (object)$row['relation']]);

$grid = new GridView([
'dataProvider' => new ArrayDataProvider(
[
'allModels' => [
$row,
],
]
),
]);

$columns = $grid->columns;
$this->assertCount(count($row) - 2, $columns);

foreach ($columns as $index => $column) {
$this->assertInstanceOf(DataColumn::className(), $column);
$this->assertArrayHasKey($column->attribute, $row);
$this->assertNotEquals('relation', $column->attribute);
$this->assertNotEquals('otherRelation', $column->attribute);
}
}
}

0 comments on commit 757b3de

Please sign in to comment.