Yii2,如何在rest请求中使用or运算符
我试图在rest请求Yii2中使用or运算符,但无法成功。 每次我出现此错误时: [ { “字段”:“过滤器”, “消息”:“运算符”或“运算符”需要多个操作数 } ] 我测试了一些东西,但没有任何效果 我想过滤 statut=0或statut=1 你知道还是我能做到 我试着 但它不起作用 以下是控制器中管理此请求的方法:Yii2,如何在rest请求中使用or运算符,yii2,Yii2,我试图在rest请求Yii2中使用or运算符,但无法成功。 每次我出现此错误时: [ { “字段”:“过滤器”, “消息”:“运算符”或“运算符”需要多个操作数 } ] 我测试了一些东西,但没有任何效果 我想过滤 statut=0或statut=1 你知道还是我能做到 我试着 但它不起作用 以下是控制器中管理此请求的方法: public function actionIndex() { return ActionsHelper::actionIndex( $this-&
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;
}
}
您使用ActiveDataFilter的方法是正确的,但是从get构建阵列的过程是这样的(示例来自我的控制器):
因此,对于您的示例,应如下所示:
http://url/api/tickets/gestions?filter[or][0][statut]=0&filter[or][1][statut]=1
这是为我构建一个有效的“或”过滤器的方法。你能显示代码吗?实际上,我不需要为此显示任何代码。这只是一个url,我正在尝试使用提供的筛选(如果您写下您尝试了,但出现了一些错误,那么就有一些东西可以显示)我用代码编辑了我的帖子,也感谢您的回答添加搜索模型。对我来说,我曾担任过筛选器[或][[status]=0&筛选器[或][[status][like]=1
http://url/api/tickets/gestions?filter[or][0][statut]=0&filter[or][1][statut]=1