Filtering models - aPisC/phroper GitHub Wiki

Querying and filtering models is achived by associated arrays. By default the keys in the array references the field, and checks equality with the given value. You can refer to fields in relation models with the field names separated with . character. It is able to apply operators to fields other than equality check with suffixes on the field names. If the query array contains more item, they will be combined with and operator by default.

Examples of data comparing operators

  • ["id" => 3]: the id of the entity has to be exactly 3
  • ["id_gt" => 3]: the id of the entity has to be greater than 3 (other number comparison operators: _ge, _lt, _le)
  • ["id_ne"=> 3]: the id of the entity not equals 3
  • ["username" => "joe", "role": 1]: selects a user with username equals joe, and the role of the user is 1
  • ["username" => "joe", "role.isDefault": true]: selects a user with the given username, and the role that the user belong is marked as isDefault
  • ["name_null"=> true]: select entities where the name is null
  • ["name_null"=> false]: select entities where the name is not null
  • ["id_in"=> [1, 3, 5] ]: select entities with ids in the specified list
  • ["id_notin"=> [1, 3, 5] ]: select entities with ids not in the specified list

Limiting and Sorting

  • ["_limit"=> 4 ]: limits the number of selected entities to 4
  • ["_start"=> 4 ]: sets the offset in SQL query
  • ["_sort"=> "name,id:desc"]: order the items by name, then by id descending
  • ["id_sort"=> 10]: sorting entities to start with id equals 10

Complex queries

  • ["_or" => ["name"=> $p, "email" => $p]]: selects the entities that the name, or the email member equals the given parameter
  • ["role.isDefault"=> true, "_or" => ["name"=> $p, "email" => $p]]: entities that has the role marked as default and the name or username is equals the parameter
  • ["_not" => ["name"=> $p, "email" => $p]]: entities where not (name and email equals the given parameter)
  • ["_and" => ["name"=> $p], ["email" => $p](/aPisC/phroper/wiki/"name"=>-$p],-["email"-=>-$p)]: entities where the name and the email equals the given parameter