1. 程式人生 > >Yii2 SqlDataProvider 原生sql 執行sql

Yii2 SqlDataProvider 原生sql 執行sql

返回的是陣列型別

$count = Yii::$app->db->createCommand('
      SELECT COUNT() FROM user WHERE status=:status
  ', [':status' => 1])->queryScalar();
 
  $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,
    &nb