Sunday, April 8, 2018

Yii2 sqldataprovider

SqlDataProvider is a plain sql query. It is also provide pagination and sorting methods.

SqlDataProvider representing data in arrays form. You can use LIMIT, GROUP BY , ORDER BY in the statements.

How to use Sql dataprovider in Yii2

$provider = new SqlDataProvider([
    'sql' => 'SELECT * FROM customer WHERE id=:id',
    'params' => [':id' => 1],
    'sort' => [
        'attributes' => [
            'name' => [
                'asc' => ['name' => SORT_ASC, 'username' => SORT_ASC],
                'default' => SORT_DESC
            ],
        ],
    ],
    'pagination' => [
        'pageSize' => 10,
    ],
]);

// get the customer results
$data = $provider->getModels();

1 comment:

Recent Update

yii2 session handling

use yii\web\Session; $session = Yii::$app->session; // start session $session->open(); // close session $session->close(); ...

Most Search