Yii2,如何在Restful请求中使用或操作

时间:2018-07-06 07:49:31

标签: php yii2 yii2-advanced-app

我正在其余请求Yii2中尝试使用或运算符,但我无法成功。 每当我有这个错误: [     {         “ field”:“ filter”,         “ message”:“运算符”或“需要多个操作数。”     } ]

我测试了几件事,但是没有用。

我要过滤 statut = 0或statut = 1

您知道还是我可以做到?

我试图 http://url/api/tickets/gestions?filter[or][statut][statut]=[0,1]

但这不起作用

以下是控制器中管理此请求的方法:

public function actionIndex()
{
    return ActionsHelper::actionIndex(
        $this->modelClass,
        $this->modelClass . 'Search'
    );
}

$ this-> modelClass在上面定义,并且等于'api \ modules \ tickets \ models \ TicketGestion';

这是ActionsHelper :: actionIndex

public function actionIndex($model, $searchModel = null, $moreFilter = null, 
$pagination = false)
{
    $filterCondition = null;

    if ($searchModel) {
        $filter = new ActiveDataFilter([
            'searchModel' => $searchModel
        ]);


        if ($filter->load(\Yii::$app->request->get())) { 
            $filterCondition = $filter->build();
            if ($filterCondition === false) {
                return $filter;
            }
        }
    }

    $query = $model::find();

    if ($filterCondition !== null) {
        $query->andWhere($filterCondition);
    }

    if ($moreFilter !== null) {
        $query->andWhere($moreFilter);
    }

    if ($pagination !== false) {
        $pagination = [
            'pageSize' => 100
        ];
    }

    return new ActiveDataProvider([
        'query' => $query,
        'pagination' => $pagination
    ]);
}

这是Gii生成的搜索模型

<?php

namespace api\modules\tickets\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use api\modules\tickets\models\TicketGestion;

/**
 * TicketGestionSearch represents the model behind the search form of `api\modules\tickets\models\TicketGestion`.
 */
class TicketGestionSearch extends TicketGestion
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'priorite', 'quicree', 'quirea', 'statut', 'recurrentid', 'typerea', 'client'], 'integer'],
            [['dispatch', 'service', 'type', 'sujet', 'datecrea', 'dateecheance', 'daterea'], 'safe'],
            [['duree'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    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 = TicketGestion::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;
        }

        if ($this->dispatch == 'null') {
            $this->dispatch = 1;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'priorite' => $this->priorite,
            'quicree' => $this->quicree,
            'quirea' => $this->quirea,
            'datecrea' => $this->datecrea,
            'dateecheance' => $this->dateecheance,
            'daterea' => $this->daterea,
            'duree' => $this->duree,
            'statut' => $this->statut,
            'recurrentid' => $this->recurrentid,
            'typerea' => $this->typerea,
            'client' => $this->client,
        ]);

        $query->andFilterWhere(['like', 'service', $this->service])
            ->andFilterWhere(['like', 'type', $this->type])
            ->andFilterWhere(['like', 'sujet', $this->sujet])
            ->andFilterWhere(['likes', 'dispatch', $this->dispatch]);

        return $dataProvider;
    }
}

0 个答案:

没有答案