A
A
akula222016-03-15 10:43:00
Yii
akula22, 2016-03-15 10:43:00

SqlDataProvider and searchModel how?

In ActiveDataProvider I used in search model

$query = self::find().....
$query->andFilterWhere(['like', 'username', $this->username]);

how to do the same in SqlDataProvider ?
As I understand it, this is no longer possible, but how is the search (filtering) done in the SqlDataProvider

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
matperez, 2016-03-15
@matperez

The Query class is able to render in SQL, so you can do something like the one below, but I haven't tried it myself, it might not work...

$query = new Query();
$query->andWhere(['some' => $this->some]);
// ... тут всякие другие условия
$dataProvider = new SqlDataProvider([
   'totalCount' => $query->count(),
   'sql' => $query->createCommand()->sql, 
]);

A
Andrey, 2016-03-15
@VladimirAndreev

so the SqlDataProvider expects a query either a query builder object

N
Nikita, 2016-03-15
@bitver

www.yiiframework.com/doc-2.0/yii-data-sqldataprovi...

$dataProvider = new SqlDataProvider([
    'sql' => 'SELECT * FROM user WHERE status=:status',
    'params' => [':status' => 1],  // <--------------
    'totalCount' => $count,
    'sort' => [
        'attributes' => [
            'age',
            'name' => [
                'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC],
                'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC],
                'default' => SORT_DESC,
                'label' => 'Name',
            ],
        ],
    ],
    'pagination' => [
        'pageSize' => 20,
    ],
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question