GET请求中的YII2 REST过滤器

时间:2018-02-15 07:31:09

标签: rest yii2 filtering

我阅读了文档,但找不到任何完整的示例。

Yii版本2.0.13.1。

所以,我想在GET请求中过滤sitePages,例如:

php -r "echo http_build_query(['filter'=>['id'=>5]]);"

我们有:

filter%5Bid%5D=5

然后我们打电话:

curl -H "Accept: */*" "http://mysite/sitepage?filter%5Bid%5D=5

我用GII创建了搜索模型:

class SitePageSearch extends SitePage
{
    public function rules()
    {
        return [
                  [['id', 'sort', 'parent_id', 'active', 'bank_id'], 'integer'],
                  [['title', 'description', 'url', 'keywords', 'created_at', 'update_at'], 'safe'],
        ];
    }

    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = SitePage::find();

        // add conditions that should always apply here

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

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'sort' => $this->sort,
            'parent_id' => $this->parent_id,
            'active' => $this->active,
            'bank_id' => $this->bank_id,
            'created_at' => $this->created_at,
            'update_at' => $this->update_at,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'url', $this->url])
            ->andFilterWhere(['like', 'keywords', $this->keywords]);

        return $dataProvider;
    }

}

和控制器:

class SitepageController extends SiteController
{
    public $modelClass = 'app\models\SitePage';

    public function actions(){
        $actions =  parent::actions();

        $actions ['index']['prepareDataProvider'] = function ($action) {
            $model = new SitePageSearch();
            \Yii::info('query Params:'.print_r(\Yii::$app->request->queryParams, 1) );
            return $model->search(\Yii::$app->request->queryParams);
            };
        return $actions;

    }
}

我在yii \ base \ Model:

的方法加载中添加了调试信息
public function load($data, $formName = null)
{
    $scope = $formName === null ? $this->formName() : $formName;
    if ($scope === '' && !empty($data)) {
        $this->setAttributes($data);
        Yii::info ('set attributes '. print_r($data, 1));
        return true;
    } elseif (isset($data[$scope])) {
        $this->setAttributes($data[$scope]);

        Yii::info ('set attributes scope '.$scope.'=>'. print_r($data, 1));
        return true;
    }
    Yii::info('my scope:'.$scope.', return false');
    return false;
}

然后在日志中我看到:

我的范围:SitePageSearch,返回false

好的,让我们按照Internet中的一些建议,使用空的formName参数写入加载:

$this->load($params, '');

然后在日志中我们看到: 无法在'app \ models \ search \ SitePageSearch

中设置不安全的属性'过滤器'

我有答案中的所有记录,过滤器不适用。

有什么问题?

0 个答案:

没有答案