2017-02-22 70 views
1

我有一個模型(但我dont't有一個桌子後面,因爲我要選擇有關數據庫本身的信息):Yii2的GridView +過濾不ActiveRecord的

namespace app\models; 

use Yii; 
use yii\base\Model; 

class OracleTables extends Model { 

    public $table_name; 

    public static function getDb() {return Yii::$app->get('db_o');} 
} 

控制器:

namespace app\controllers; 

use app\models\OracleTablesSearch; 
use yii\web\Controller; 
use yii\helpers\Url; 
use dmstr\bootstrap\Tabs; 

class OracleTablesController extends Controller { 

    public function actionIndex() { 
     $searchModel = new OracleTablesSearch; 
     $dataProvider = $searchModel->search($_GET); 

     Tabs::clearLocalStorage(); 

     Url::remember(); 
     \Yii::$app->session['__crudReturnUrl'] = null; 

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

} 

搜索模型:

namespace app\models; 

use Yii; 
use yii\base\Model; 
use app\models\OracleTables; 
use yii\data\SqlDataProvider; 

class OracleTablesSearch extends OracleTables { 

    public function rules() {return [[['table_name'], 'safe'],];} 

    public function search($params) { 
     $totalCount = Yii::$app->db_o 
       ->createCommand('SELECT COUNT(TABLE_NAME) FROM USER_TABLES WHERE NUM_ROWS > 0') 
       ->queryScalar(); 

     $dataProvider = new SqlDataProvider([ 
      'db' => Yii::$app->db_o, 
      'sql' => 'SELECT TABLE_NAME FROM USER_TABLES WHERE NUM_ROWS > 0 ORDER BY TABLE_NAME ASC', 
      'totalCount' => $totalCount, 
     ]); 

     $this->load($params); 

     return $dataProvider; 
    } 

} 

視圖文件:

GridView::widget([ 
    'dataProvider' => $dataProvider, 
    'pager' => ['class' => yii\widgets\LinkPager::className(), 'firstPageLabel' => Yii::t('app', 'First'), 'lastPageLabel' => Yii::t('app', 'Last'),], 
    'filterModel' => $searchModel, 
    'tableOptions' => ['class' => 'table table-striped table-bordered table-hover'], 
    'headerRowOptions' => ['class' => 'x'], 
    'columns' => [ 
     [ 
      'attribute' => 'TABLE_NAME', 
      'contentOptions' => ['nowrap' => 'nowrap'], 
      'filter' => AutoComplete::widget([ 
       'model' => $searchModel, 
       'attribute' => 'table_name', 
       'clientOptions' => [ 
        'source' => [], 
        'autoFill' => true, 
        //'minLength' => 2 
       ], 
       'options' => ['class' => 'form-control'] 
      ]), 
     ], 
    ], 
]); 

它的工作原理,但沒有過濾,我也想過濾。我知道如何填充過濾器來源,這不是問題。問題是,我無法將$sql->andFilterWhere(['like', 'table_name', $this->table_name]);添加到搜索模型,因爲那時我需要在SqlDataProvider之外傳輸sql命令,但之後它不再工作。看看這個,請(搜索模式):

public function search($params) { 
    $totalCount = Yii::$app->db_o 
      ->createCommand('SELECT COUNT(TABLE_NAME) FROM USER_TABLES WHERE NUM_ROWS > 0') 
      ->queryScalar(); 
    $sql = Yii::$app->db_o 
      ->createCommand('SELECT TABLE_NAME FROM USER_TABLES WHERE NUM_ROWS > 0 ORDER BY TABLE_NAME ASC') 
      ->queryColumn(); 

    $dataProvider = new SqlDataProvider([ 
     'db' => Yii::$app->db_o, 
     'sql' => $sql, 
     'totalCount' => $totalCount, 
    ]); 

    $this->load($params); 

    $sql->andFilterWhere(['like', 'table_name', $this->table_name]); 

    return $dataProvider; 
} 

如果我做像這樣,我得到了以下錯誤消息:

PHP的警告

的preg_match()預計參數2是字符串,數組給定

在... \廠商\ yiisoft \ yii2 \數據\ SqlDataProvider.php

131}

$sql = $this->sql; 
    $orders = []; 
    $limit = $offset = null; 

    if ($sort !== false) { 
     $orders = $sort->getOrders(); 
     $pattern = '/\s+order\s+by\s+([\w\s,\.]+)$/i'; 
     if (preg_match($pattern, $sql, $matches)) { 
      array_unshift($orders, new Expression($matches[1])); 
      $sql = preg_replace($pattern, '', $sql); 
     } 
    } 

    if ($pagination !== false) { 
     $pagination->totalCount = $this->getTotalCount(); 
     $limit = $pagination->getLimit(); 
     $offset = $pagination->getOffset(); 

我不覺得有什麼參數,他談論。或者我應該使用什麼樣的dataProvider呢?你能指點我正確的方向嗎?非常感謝!

回答

1

我已經解決了這種方式:

public $table_name; 

public function search($params) { 
    isset($_GET['wts']['table_name']) ? $table_name = $_GET['wts']['table_name'] : $table_name = ""; 

    $totalCount = Yii::$app->db_o 
      ->createCommand('SELECT COUNT(TABLE_NAME) FROM USER_TABLES WHERE NUM_ROWS > 0 AND LOWER(TABLE_NAME) LIKE LOWER(:table_name)') 
      ->bindValue(':table_name', '%' . $table_name . '%') 
      ->queryScalar(); 

    $dataProvider = new SqlDataProvider([ 
     'db' => Yii::$app->db_o, 
     'sql' => 'SELECT TABLE_NAME, NUM_ROWS FROM USER_TABLES WHERE NUM_ROWS > 0 AND LOWER(TABLE_NAME) LIKE LOWER(:table_name) ORDER BY TABLE_NAME ASC', 
     'params' => [':table_name' => '%' . $table_name . '%'], 
     'totalCount' => $totalCount, 
     'pagination' => [ 
      'pageSize' => 15, 
     ], 
    ]);