diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 3966300..b7668f8 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -5,7 +5,8 @@ Yii Framework 2 Change Log 2.0.9 under development ----------------------- -- Enh #4146: Added `yii\data\ArrayDataProvider::$modelClass` property to specify a model used to provide column labels even when data array is empty (PowerGamer1) +- Enh #11490: Added `yii\data\ArrayDataProvider::$modelClass` property to specify a model used to provide column labels even when data array is empty (PowerGamer1) +- Bug #9950: Updated `yii\grid\DataColumn::getHeaderCellLabel()` to extract attribute label from the `filterModel` of Grid (silverfire) - Enh #11428: Speedup SQL query in `yii\db\oci\Schema::findColumns()` (SSiwek) - Enh #11414: Files specified as `null` in `yii\web\AssetBundle` won't be registered (Razzwan) - Enh #11432: Added HTTP status 421 "Misdirected Request" to list of statuses in `yii\web\Response` (dasmfm) diff --git a/framework/grid/DataColumn.php b/framework/grid/DataColumn.php index 8eeea14..dd06283 100644 --- a/framework/grid/DataColumn.php +++ b/framework/grid/DataColumn.php @@ -148,6 +148,8 @@ class DataColumn extends Column /* @var $model Model */ $model = new $provider->modelClass; $label = $model->getAttributeLabel($this->attribute); + } else if ($this->grid->filterModel !== null && $this->grid->filterModel instanceof Model) { + $label = $this->grid->filterModel->getAttributeLabel($this->attribute); } else { $models = $provider->getModels(); if (($model = reset($models)) instanceof Model) { diff --git a/tests/framework/grid/DataColumnTest.php b/tests/framework/grid/DataColumnTest.php index b9eb04e..f39286b 100644 --- a/tests/framework/grid/DataColumnTest.php +++ b/tests/framework/grid/DataColumnTest.php @@ -20,7 +20,7 @@ class DataColumnTest extends \yiiunit\TestCase $this->mockApplication(); } - public function testColumnLabelsOnEmptyProvider() + public function testColumnLabelsOnEmptyArrayProvider() { $grid = new GridView([ 'dataProvider' => new ArrayDataProvider([ @@ -41,4 +41,26 @@ class DataColumnTest extends \yiiunit\TestCase $this->assertEquals(['Customer', 'Invoice Total'], $labels); } + + public function testColumnLabelsOnEmptyArrayProviderWithFilterModel() + { + $grid = new GridView([ + 'dataProvider' => new ArrayDataProvider([ + 'allModels' => [], + 'totalCount' => 0, + ]), + 'columns' => ['customer_id', 'total'], + 'filterModel' => new Order + ]); + + $labels = []; + foreach ($grid->columns as $column) { + $method = new \ReflectionMethod($column, 'getHeaderCellLabel'); + $method->setAccessible(true); + $labels[] = $method->invoke($column); + $method->setAccessible(false); + } + + $this->assertEquals(['Customer', 'Invoice Total'], $labels); + } }