zhoutengfu 2017-08-07 17:36:21 430次浏览 0条回复 0 0 0

第一步、将search方法添加到项目中,我在项目的根目录中添加actions文件夹,再讲search方法添加进去,search方法代码

<?php
/**
 * created by zhoutengfu.
 * date: 11/26/15 02:14
 * func:
 * description:
 */
namespace app\actions;

use Yii;
use yii\rest\Action;
use yii\data\ActiveDataProvider;

class SearchAction extends Action{
    /**
     * @var callable a PHP callable that will be called to prepare a data provider that
     * should return a collection of the models. If not set, prepareDataProvider() will be used instead.
     * The signature of the callable should be:
     *
     * ```php
     * function ($action) {
     *     // $action is the action object currently running
     * }
     * ```
     *
     * The callable should return an instance of ActiveDataProvider.
     */
    public $prepareDataProvider;
    public $params;
    /**
     * @return ActiveDataProvider
     */
    public function run() {
        if ($this->checkAccess) {
            call_user_func($this->checkAccess, $this->id);
        }
        return $this->prepareDataProvider();
    }
    /**
     * Prepares the data provider that should return the requested collection of the models.
     * @return ActiveDataProvider
     */
    protected function prepareDataProvider() {
        if ($this->prepareDataProvider !== null) {
            return call_user_func($this->prepareDataProvider, $this);
        }

        /**
         * @var \yii\db\BaseActiveRecord $modelClass
         */
        $modelClass = $this->modelClass;
        $model = new $this->modelClass([
        ]);
        $safeAttributes = $model->safeAttributes();

        $params = array();
        $keyword = '';
        $order_by = $model->orderBy();

        foreach($this->params as $key => $value){
            if(in_array($key, $safeAttributes)){
                $params[$key] = $value;
            }elseif($key == 'keyword'){
                $keyword = $value;
                continue;
            }elseif($key == 'order-by'){
                $order_by = $value;
            }
        }
        $query = $modelClass::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (empty($params) && !$keyword && !$order_by) {
            return $dataProvider;
        }
        if($keyword && $modelClass::searchFields()){
            $index = 0;
            foreach($modelClass::searchFields() as $i){
                if($index == 0) {
                    $query->andFilterWhere(['like', $i, $keyword]);
                }else{
                    $query->orFilterWhere(['like',$i,$keyword]);
                }
                $index++;
            }
        }

        foreach ($params as $param => $value) {
            $query->andFilterWhere([
                $param => $value,
            ]);
        }
        if($order_by){
            $query->addOrderBy($order_by);
        }
        return $dataProvider;
    }
}

第二步、BaseController添加

public function actions(){
        $actions = parent::actions();
        $actions['search'] = [
            'class' => 'app\actions\SearchAction',
            'modelClass' => $this->modelClass,
            'checkAccess' => [$this, 'checkAccess'],
            'params' => Yii::$app->request->get()
        ];
        return $actions;
    }
    public function verbs() {
        $verbs = [
            'search' => ['GET']
        ];
        return array_merge(parent::verbs(), $verbs);
    }

第三步、model中添加

public static function searchFields()
{
    return ['搜索的字段'];
}

第四步、就可以通过keyword来进行搜索了

    没有找到数据。
您需要登录后才可以回复。登录 | 立即注册