Skip to content

Latest commit

 

History

History
83 lines (60 loc) · 1.37 KB

request-options.md

File metadata and controls

83 lines (60 loc) · 1.37 KB

Request Options

In order to customize the request you can define multiple filter options:

Sort

Set a sort order for a given field.

setSort(['id' => SORT_ASC]);

or the opposite way

setSort(['id' => SORT_DESC]);
  • SORT_ASC = 1,2,3
  • SORT_DESC = 3,2,1

Filter

The filters must be enabled on the api side, otherwise it wont have any effect.

Example usage assuming filters are configured on api:

setFilter(['lang_id' => 1]); // like a where condition lang_id=1

greather then, smaller then operators:

setFilter(['publication_date' => ['lt' => strtotime('tomorrow'), 'gt' => strtotime('yesterday')]);  //like >= and <= conditions for two fields.

Example using the in condition for both languages:

setFilter([
    'lang_id' => ['in' => [1,2]]
]);

All posible operators:

  • and (AND)
  • or (OR)
  • not (NOT)
  • lt (<)
  • gt (>)
  • lte (<=)
  • gte (>=)
  • eq (=)
  • neq (!=)
  • in (IN)
  • nin (NOT IN)
  • like (LIKE)

Conditions

combine two conditions with which are AND conditions:

setFilter([
    'publication_date' => ['lt' => strtotime('tomorrow'), 'gt' => strtotime('yesterday')],
    'lang_id' => 2
]);

Two conditions but connected as OR condition:

setFilter([
'or' => [
    ['lang_id' => 1],
    ['publication_date' => ['gt' => time()]],
]
])

@see https://www.yiiframework.com/doc/api/2.0/yii-data-datafilter