Browse Source

Fixes #12828: Fixed handling of nested arrays, objects in `\yii\grid\GridView::guessColumns`

tags/2.0.11
Evgeniy Tkachenko 8 years ago committed by Alexander Makarov
parent
commit
757b3de0bf
  1. 1
      framework/CHANGELOG.md
  2. 2
      framework/grid/GridView.php
  3. 62
      tests/framework/grid/GridViewTest.php

1
framework/CHANGELOG.md

@ -65,6 +65,7 @@ Yii Framework 2 Change Log
- Bug #12605: Make 'safe' validator work on write-only properties (arthibald, CeBe) - 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 #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 #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 #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 #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) - Enh #8719: Add support for HTML5 attributes on submitbutton (formaction/formmethod...) for ActiveForm (VirtualRJ)

2
framework/grid/GridView.php

@ -569,8 +569,10 @@ class GridView extends BaseListView
$model = reset($models); $model = reset($models);
if (is_array($model) || is_object($model)) { if (is_array($model) || is_object($model)) {
foreach ($model as $name => $value) { foreach ($model as $name => $value) {
if ($value === null || is_scalar($value) || is_callable([$value, '__toString'])) {
$this->columns[] = (string) $name; $this->columns[] = (string) $name;
} }
} }
} }
}
} }

62
tests/framework/grid/GridViewTest.php

@ -0,0 +1,62 @@
<?php
namespace yiiunit\framework\grid;
use yii\data\ArrayDataProvider;
use yii\grid\DataColumn;
use yii\grid\GridView;
/**
* @author Evgeniy Tkachenko <et.coder@gmail.com>
* @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);
}
}
}
Loading…
Cancel
Save