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", "ingo");
$authorTpl1->setValue("email", "LIKE '%wemove%'");
// OR Author.name LIKE '%herwig%'
$authorTpl2 = $query->getObjectTemplate('Author', null, Criteria::OPERATOR_OR);
$authorTpl2->setValue("name", "herwig");
// Recipe.created >= '2004-01-01' AND Recipe.created < '2005-01-01'
$recipeTpl1 = $query->getObjectTemplate('Recipe');
$recipeTpl1->setValue("created", ">= '2004-01-01'");
$recipeTpl2 = $query->getObjectTemplate('Recipe');
$recipeTpl2->setValue("created", "< '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", "Salat");
$recipeTpl4 = $query->getObjectTemplate('Recipe', null, Criteria::OPERATOR_OR);
$recipeTpl4->setValue("portions", "= 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 105 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 ()
 
 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

 getQueryType ()
 
 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
 getQueryType ()
 
 buildQuery ($buildDepth, $orderby=null, PagingInfo $pagingInfo=null)
 
 executeInternal (SelectStatement $selectStmt, $buildDepth, PagingInfo $pagingInfo=null)
 

Additional Inherited Members

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

Constructor & Destructor Documentation

__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 129 of file ObjectQuery.php.

__destruct ( )

Destructor.

Definition at line 142 of file ObjectQuery.php.

Member Function Documentation

getId ( )

Get the query id.

Returns
String

Definition at line 151 of file ObjectQuery.php.

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 163 of file ObjectQuery.php.

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 197 of file ObjectQuery.php.

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 236 of file ObjectQuery.php.

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 254 of file ObjectQuery.php.

getQueryType ( )
protected
See also
AbstractQuery::getQueryType()

Definition at line 267 of file ObjectQuery.php.

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

Definition at line 274 of file ObjectQuery.php.

processObjectTemplate ( PersistentObject  $tpl,
SelectStatement  $selectStmt 
)
protected

Process an object template.

Parameters
$tplThe object template
$selectStmtA SelectStatement instance

Definition at line 337 of file ObjectQuery.php.

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 466 of file ObjectQuery.php.

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 542 of file ObjectQuery.php.

getParameterPosition (   $criterion,
  $criteria 
)
protected

Definition at line 581 of file ObjectQuery.php.

resetInternals ( )
protected

Reset internal variables.

Must be called after buildQuery

Definition at line 598 of file ObjectQuery.php.

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 611 of file ObjectQuery.php.

valueChanged ( ValueChangeEvent  $event)

Listen to ValueChangeEvents.

Parameters
$eventValueChangeEvent instance

Definition at line 639 of file ObjectQuery.php.

Member Data Documentation

const PROPERTY_COMBINE_OPERATOR = "object_query_combine_operator"

Definition at line 107 of file ObjectQuery.php.

const PROPERTY_TABLE_NAME = "object_query_table_name"

Definition at line 108 of file ObjectQuery.php.

const PROPERTY_INITIAL_OID = "object_query_initial_oid"

Definition at line 109 of file ObjectQuery.php.