Developers/Library/Conditions for searching

From Aimeos documentation

Developers
Other languages:
English 100%


Conditions for searching

The searchItems() method of all managers requires a criteria object whose conditions are used to limit the result set only to the necessary entries. The conditions are in an abstract notation which is translated to the real notation of the underlying storage (e.g. SQL for databases) before sending the query to the storage manager (the database server in case of SQL).

There are two types of operators available, one for comparing items against values and one for combining one or more terms (compare or further combine terms).

List of combination operators:

  • "&&": term1 AND term2 (AND term3)*
  • "||": term1 OR term2 (OR term3)*
  • "!": NOT term (allows only one term)

List of comparison operators:

  • "==": item EQUAL value (or list of values)
  • "!=": item NOT EQUAL value (or list of values)
  • "~=": item LIKE value
  • ">=": item GREATER OR EQUAL value
  • "<=": item SMALLER OR EQUAL value
  • ">": item GREATER value
  • "<": item SMALLER value

With these operators, you can build conditions of any complexity, e.g.

  1. $search = $productManager->createSearch( true );
  2. $expr = array(
  3.     $search->compare( '==', 'product.id', <array of IDs> ),
  4.     $search->getConditions(),
  5.     $search->compare( '>=', 'product.mtime', '2012-01-01 00:00:00' ),
  6. );
  7. $search->setConditions( $search->combine( '&&', $expr );
  8.  
  9. $productManager->searchItems( $search );

This code block would search for all products whose ID is in the list of IDs and which has a modification time later than begin of 2012. The result would also be limited by the default conditions (which are filtering by "product.status", "product.datestart" and "product.dateend") because of the "true" parameter in the createSearch() call and the $search->getConditions() as first expression.