Yii2: ActiveDataProvider using $dataProvider->keys after search no effect of appended query.

Created on 2 Jan 2017  路  2Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

$searchModel = new SearchModel();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

if (!empty(Yii::$app->request->queryParams)) {
    // $dataProvider->keys = [1, 2, 3]
    $IDArray = Model::someFunction($dataProvider->keys);
    $dataProvider->query->andWhere(['NOT IN', 'id', $IDArray]); // $IDArray = [1,2]
}

return $this->render('index', [
    'searchModel' => $searchModel,
    'dataProvider' => $dataProvider,
]);

What is the expected result?

$dataProvider->keys = [3];

What do you get instead?

$dataProvider->keys = [1, 2, 3];

Additional info

Although checked that query appended correctly with $dataProvder->query

    yii\db\ActiveQuery#1
    (
        [sql] => null
        [on] => null
        [select] => null
        [selectOption] => null
        [distinct] => null
        [from] => null
        [groupBy] => null
        [join] => null
        [having] => null
        [union] => null
        [params] => []
        [yii\base\Component:_events] => []
        [yii\base\Component:_behaviors] => []
        [where] => [
            0 => 'and'
            1 => [
                'test_id' => '2'
            ]
            2 => [
                0 => 'NOT IN'
                1 => 'id'
                2 => [
                    0 => 1
                    1 => 2
                ]
            ]
        ]
..
..
..
..

For temporary Workaround using

$clone = clone $dataProvider;
$IDArray = Model::someFunction($clone->keys);

| Q | A
| ---------------- | ---
| Yii version | 2.0.9
| PHP version |7.0.13-1+deb.sury.org~precise+1
| Operating system |Ubuntu

Most helpful comment

After accessing keys they are cached to avoid duplicate queries. to refresh the data in the dataprovider, call $dataProvider->refresh(). http://www.yiiframework.com/doc-2.0/yii-data-basedataprovider.html#refresh%28%29-detail

All 2 comments

After accessing keys they are cached to avoid duplicate queries. to refresh the data in the dataprovider, call $dataProvider->refresh(). http://www.yiiframework.com/doc-2.0/yii-data-basedataprovider.html#refresh%28%29-detail

@cebe Thanks. Should read guide properly. Sorry for lame issue.

Was this page helpful?
0 / 5 - 0 ratings