Yii2: Bug when sorting: Undefined index: date

Created on 20 Oct 2014  路  6Comments  路  Source: yiisoft/yii2

Undefined index: date

In /vendor/yiisoft/yii2/data/Sort.php(213) $this->attributes is empty.

use app\models\ArcCandidate;
use Yii;
use yii\rest\ActiveController;
use yii\data\ActiveDataProvider;
use yii\sphinx\Query;


class ArcCandidateController extends ActiveController
{
    public $modelClass = 'app\models\ArcCandidate';

    public function actionSearch($q)
    {
        $query = (new Query())->select([
            'id',
            'date',
        ])->from(ArcCandidate::indexName())->match($q);

        $provider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'defaultPageSize' => 10,
            ],
            'sort'=> [
                'defaultOrder' => [
                    'date' => SORT_DESC,
                ],
            ],
        ]);

        return $provider;
    }
}
<response>
<name>PHP Notice</name>
<message>Undefined index: date</message>
<code>8</code>
<type>yii\base\ErrorException</type>
<file>
...../vendor/yiisoft/yii2/data/Sort.php
</file>
<line>213</line>
<stack-trace>
<item>
#0 ...../vendor/yiisoft/yii2/data/Sort.php(213): yii\base\ErrorHandler->handleError(8, 'Undefined index...', '/var/www/vhosts...', 213, Array)
</item>
<item>
#1 ...../vendor/yiisoft/yii2/data/ActiveDataProvider.php(108): yii\data\Sort->getOrders()
</item>
<item>
#2 ...../vendor/yiisoft/yii2/data/BaseDataProvider.php(79): yii\data\ActiveDataProvider->prepareModels()
</item>
<item>
#3 ...../vendor/yiisoft/yii2/data/BaseDataProvider.php(92): yii\data\BaseDataProvider->prepare()
</item>
<item>
#4 ...../vendor/yiisoft/yii2/rest/Serializer.php(162): yii\data\BaseDataProvider->getModels()
</item>
<item>
#5 ...../vendor/yiisoft/yii2/rest/Serializer.php(131): yii\rest\Serializer->serializeDataProvider(Object(yii\data\ActiveDataProvider))
</item>
<item>
#6 ...../vendor/yiisoft/yii2/rest/Controller.php(97): yii\rest\Serializer->serialize(Object(yii\data\ActiveDataProvider))
</item>
<item>
#7 ...../vendor/yiisoft/yii2/rest/Controller.php(75): yii\rest\Controller->serializeData(Object(yii\data\ActiveDataProvider))
</item>
<item>
#8 ...../vendor/yiisoft/yii2/base/Controller.php(153): yii\rest\Controller->afterAction(Object(yii\base\InlineAction), Object(yii\data\ActiveDataProvider))
</item>
<item>
#9 ...../vendor/yiisoft/yii2/base/Module.php(455): yii\base\Controller->runAction('search', Array)
</item>
<item>
#10 ...../vendor/yiisoft/yii2/web/Application.php(83): yii\base\Module->runAction('arc-candidate/s...', Array)
</item>
<item>
#11 ...../vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
</item>
<item>
#12 ...../web/index.php(13): yii\base\Application->run()
</item>
<item>#13 {main}</item>
</stack-trace>
</response>
bug

Most helpful comment

don't underestimate the time I put in doing investigation. It seems that I missed my own comment in this topic.

This is a clear example that we should put more effort in the creation of good titles.

All 6 comments

attributes is empty by default, GridView can only fill it with default values when the Query is an ActiveQuery so it can use the AR attributes. When you use a simple Query object, you have to configure sort attributes yourself.

this issue can still exist when using ActiveQuery in the scenario that $sort is defined in the configuration array, before query. It was timeconsuming to understand the root of this issue.

@dynasource thanks for reporting this. I think it could be fixed by making the logic of getSort() and setSort() in dataprovider to be lazy, i.e. moving the creation of the sorting object from setSort() to getSort().

@dynasource, please be more careful while moving issues to another extrension bug tracking.
Presence of the some extension keywords does not mandatory means it to this extension.

It is pity that dispite your own investigation on this matter:
https://github.com/yiisoft/yii2/issues/5655#issuecomment-252988776
you still missplace the issue.

don't underestimate the time I put in doing investigation. It seems that I missed my own comment in this topic.

This is a clear example that we should put more effort in the creation of good titles.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

indicalabs picture indicalabs  路  3Comments

MUTOgen picture MUTOgen  路  3Comments

skcn022 picture skcn022  路  3Comments

psfpro picture psfpro  路  3Comments

jpodpro picture jpodpro  路  3Comments