Detailed Description

ObjectQuery implements a template based object query.

This class provides the user with object templates on which query conditions may be set. Object templates are Node instances whose attribute values are used as conditions on the appropriate attributes. A value maybe a scalar or a Criteria instance. For example $authorTpl->setValue("name", Criteria::forValue("LIKE", "%ingo%") means searching for authors whose name contains 'ingo'. If only a scalar is given LIKE '%...' is assumed.

A value condition of a template is joined with the preceeding conditions using the combine operator (Criteria::OPERATOR_AND, Criteria::OPERATOR_OR) given in the Criteria assigned to the template value. The set of conditions of a template is preceded by the operator (Criteria::OPERATOR_AND, Criteria::OPERATOR_OR) given in the ObjectQuery::PROPERTY_COMBINE_OPERATOR property (default: Criteria::OPERATOR_AND) of the template (see ObjectQuery::getObjectTemplate()).

Multiple conditions for one value are built using different object templates of the same type. Conditions sets of different object templates are grouped with brackets if they are passed to ObjectQuery::makeGroup().

Note
: If there are two object templates of the same type as the query type linked in a parent child relation, than the nodes that are selected depend on the attributes of the first object template that is received by ObjectQuery::getObjectTemplate.
: The query does not search in objects, that are created inside the current transaction.

The following example shows the usage:

// The code builds the following query condition:
// WHERE (Author.name LIKE '%ingo%' AND Author.email LIKE '%wemove%') OR (Author.name LIKE '%herwig%') AND
// (Recipe.created >= '2004-01-01') AND (Recipe.created < '2005-01-01') AND ((Recipe.name LIKE '%Salat%') OR (Recipe.portions = 4))
$query = new ObjectQuery('Author');
// (Author.name LIKE '%ingo%' AND Author.email LIKE '%wemove%')
$authorTpl1 = $query->getObjectTemplate('Author');
$authorTpl1->setValue("name", Criteria::asValue("LIKE", "%ingo%"));
$authorTpl1->setValue("email", Criteria::asValue("LIKE", "%wemove%"));
// OR Author.name LIKE '%herwig%'
$authorTpl2 = $query->getObjectTemplate('Author', null, Criteria::OPERATOR_OR);
$authorTpl2->setValue("name", Criteria::asValue("LIKE", "%herwig%"));
// Recipe.created >= '2004-01-01' AND Recipe.created < '2005-01-01'
$recipeTpl1 = $query->getObjectTemplate('Recipe');
$recipeTpl1->setValue("created", Criteria::asValue(">=", "2004-01-01"));
$recipeTpl2 = $query->getObjectTemplate('Recipe');
$recipeTpl2->setValue("created", Criteria::asValue("<", "2005-01-01"));
// AND (Recipe.name LIKE '%Salat%' OR Recipe.portions = 4)
// could have be built using one template, but this demonstrates the usage
// of the ObjectQuery::makeGroup() method
$recipeTpl3 = $query->getObjectTemplate('Recipe');
$recipeTpl3->setValue("name", Criteria::asValue("LIKE", "%Salat%"));
$recipeTpl4 = $query->getObjectTemplate('Recipe', null, Criteria::OPERATOR_OR);
$recipeTpl4->setValue("portions", Criteria::asValue("=", 4));
$query->makeGroup([$recipeTpl3, $recipeTpl4], Criteria::OPERATOR_AND);
$authorTpl1->addNode($recipeTpl1, 'Recipe');
$authorTpl1->addNode($recipeTpl2, 'Recipe');
$authorTpl1->addNode($recipeTpl3, 'Recipe');
$authorTpl1->addNode($recipeTpl4, 'Recipe');
$authorList = $query->execute(BuildDepth::SINGLE);
Note
There are some limitations when using this class:
  • This class works only with Nodes as PersistentObjects
  • This class only works for Nodes mapped by RDBMapper subclasses.
  • All objects have to reside in the same datastore (the connection is taken from the first mapper)
  • Since the query values are set together with the operator in a single string, they must be converted to data store format already
Author
ingo herwig ingo@.nosp@m.wemo.nosp@m.ve.co.nosp@m.m

Definition at line 106 of file ObjectQuery.php.

+ Inheritance diagram for ObjectQuery:

Public Member Functions

 __construct ($type, $queryId=SelectStatement::NO_CACHE)
 
 __destruct ()
 
 getId ()
 
 getObjectTemplate ($type, $alias=null, $combineOperator=Criteria::OPERATOR_AND)
 
 registerObjectTemplate (Node $template, $alias=null, $combineOperator=Criteria::OPERATOR_AND)
 
 makeGroup ($templates, $combineOperator=Criteria::OPERATOR_AND)
 
 getQueryCondition ()
 
 getQueryType ()
 
 valueChanged (ValueChangeEvent $event)
 
- Public Member Functions inherited from AbstractQuery
 execute ($buildDepth, $orderby=null, $pagingInfo=null)
 
 getQueryString ($buildDepth=BuildDepth::SINGLE, $orderby=null)
 
 getLastQueryString ()
 

Public Attributes

const PROPERTY_COMBINE_OPERATOR = "object_query_combine_operator"
 
const PROPERTY_TABLE_NAME = "object_query_table_name"
 
const PROPERTY_INITIAL_OID = "object_query_initial_oid"
 

Protected Member Functions

 getLogger ()
 
 buildQuery ($buildDepth, $orderby=null, PagingInfo $pagingInfo=null)
 
 processObjectTemplate (PersistentObject $tpl, SelectStatement $selectStmt)
 
 processOrderBy ($orderby, SelectStatement $selectStmt)
 
 getParameters ($criteria, array $parameters)
 
 getParameterPosition ($criterion, $criteria)
 
 resetInternals ()
 
 processTableName (Node $tpl)
 
- Protected Member Functions inherited from AbstractQuery
 executeInternal (SelectStatement $selectStmt, $buildDepth, PagingInfo $pagingInfo=null)
 

Additional Inherited Members

- Static Protected Member Functions inherited from AbstractQuery
static getConnection ($type)
 
static getMapper ($type)
 

Constructor & Destructor Documentation

◆ __construct()

__construct (   $type,
  $queryId = SelectStatement::NO_CACHE 
)

Constructor.

Parameters
$typeThe type to search for
$queryIdIdentifier for the query cache (maybe null to prevent caching) (default: null)

Definition at line 132 of file ObjectQuery.php.

◆ __destruct()

__destruct ( )

Destructor.

Definition at line 148 of file ObjectQuery.php.

Member Function Documentation

◆ getId()

getId ( )

Get the query id.

Returns
String

Definition at line 157 of file ObjectQuery.php.

◆ getObjectTemplate()

getObjectTemplate (   $type,
  $alias = null,
  $combineOperator = Criteria::OPERATOR_AND 
)

Get an object template for a given type.

Parameters
$typeThe type to query for
$aliasAn alias name to be used in the query. if null, use the default name (default: null)
$combineOperatorOne of the Criteria::OPERATOR constants that precedes the conditions described in the template (default: Criteria::OPERATOR_AND)
Returns
Node

Definition at line 169 of file ObjectQuery.php.

◆ registerObjectTemplate()

registerObjectTemplate ( Node  $template,
  $alias = null,
  $combineOperator = Criteria::OPERATOR_AND 
)

Register an object template at the query.

Parameters
$templateNode instance to register as template
$aliasAn alias name to be used in the query. if null, use the default name (default: null)
$combineOperatorOne of the Criteria::OPERATOR constants that precedes the conditions described in the template (default: Criteria::OPERATOR_AND)

Definition at line 203 of file ObjectQuery.php.

◆ makeGroup()

makeGroup (   $templates,
  $combineOperator = Criteria::OPERATOR_AND 
)

Group different templates together to realize brackets in the query.

Note
Grouped templates will be ignored, when iterating over the object tree and appended at the end.
Parameters
$templatesAn array of references to the templates contained in the group
$combineOperatorOne of the Criteria::OPERATOR constants that precedes the group (default: Criteria::OPERATOR_AND)

Definition at line 242 of file ObjectQuery.php.

◆ getQueryCondition()

getQueryCondition ( )

Get the condition part of the query.

This is especially useful to build a StringQuery from the query objects.

Returns
String

Definition at line 260 of file ObjectQuery.php.

◆ getLogger()

getLogger ( )
protected
See also
AbstractQuery::getLogger()

Reimplemented from AbstractQuery.

Definition at line 273 of file ObjectQuery.php.

◆ getQueryType()

getQueryType ( )
See also
AbstractQuery::getQueryType()

Reimplemented from AbstractQuery.

Definition at line 280 of file ObjectQuery.php.

◆ buildQuery()

buildQuery (   $buildDepth,
  $orderby = null,
PagingInfo  $pagingInfo = null 
)
protected
See also
AbstractQuery::buildQuery()

Reimplemented from AbstractQuery.

Reimplemented in StringQuery.

Definition at line 287 of file ObjectQuery.php.

◆ processObjectTemplate()

processObjectTemplate ( PersistentObject  $tpl,
SelectStatement  $selectStmt 
)
protected

Process an object template.

Parameters
$tplThe object template
$selectStmtA SelectStatement instance

Definition at line 350 of file ObjectQuery.php.

◆ processOrderBy()

processOrderBy (   $orderby,
SelectStatement  $selectStmt 
)
protected

Process an object template.

Parameters
$orderbyAn array holding names of attributes to order by, maybe appended with 'ASC', 'DESC' (maybe null)
$selectStmtA SelectStatement instance

Definition at line 479 of file ObjectQuery.php.

◆ getParameters()

getParameters (   $criteria,
array  $parameters 
)
protected

Get an array of parameter values for the given criteria.

Parameters
$criteriaAn array of Criteria instances that define conditions on the object's attributes (maybe null)
$parametersArray defining the parameter order
Returns
Array

Definition at line 562 of file ObjectQuery.php.

◆ getParameterPosition()

getParameterPosition (   $criterion,
  $criteria 
)
protected

Definition at line 600 of file ObjectQuery.php.

◆ resetInternals()

resetInternals ( )
protected

Reset internal variables.

Must be called after buildQuery

Definition at line 617 of file ObjectQuery.php.

◆ processTableName()

processTableName ( Node  $tpl)
protected

Get the table name for the template and calculate an alias if necessary.

Parameters
$tplThe object template
Returns
Associative array with keys 'name', 'alias'

Definition at line 630 of file ObjectQuery.php.

◆ valueChanged()

valueChanged ( ValueChangeEvent  $event)

Listen to ValueChangeEvents.

Parameters
$eventValueChangeEvent instance

Definition at line 658 of file ObjectQuery.php.

Member Data Documentation

◆ PROPERTY_COMBINE_OPERATOR

const PROPERTY_COMBINE_OPERATOR = "object_query_combine_operator"

Definition at line 108 of file ObjectQuery.php.

◆ PROPERTY_TABLE_NAME

const PROPERTY_TABLE_NAME = "object_query_table_name"

Definition at line 109 of file ObjectQuery.php.

◆ PROPERTY_INITIAL_OID

const PROPERTY_INITIAL_OID = "object_query_initial_oid"

Definition at line 110 of file ObjectQuery.php.

static asValue($operator, $value)
Factory method for constructing a Criteria that may be used as value on a PersistentObject's attribut...
Definition: Criteria.php:58