wCMF Wiki : ObjectQuery

HomePage :: Categories :: PageIndex :: RecentChanges :: Login/Register

Description

ObjectQuery is the base class for object queries.

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 inludes the operator to be applied to it. For example $authorTpl->setValue("name", "LIKE '%ingo%'") means searching for authors whose name contains 'ingo'. Operator and value should be separated by a space. If no operator is given LIKE '%...' is assumed.

All value conditions of one template are joined with the same operator ('AND', 'OR') given in the "inter_operator" (DATATYPE_IGNORE) value of the template. The set of conditions of a template is preceded by the operator ('AND', 'OR', 'NOT') given in the "pre_operator" (DATATYPE_IGNORE) value (default: 'AND') of the template (

See also:
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().


Example

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 >= '01.01.2004') AND (Recipe.created < '01.01.2005') AND ((Recipe.name LIKE '%Salat%') OR (Recipe.portions = 4))
 
 $query = &PersistenceFacade::createObjectQuery('Author');

 // (Author.name LIKE '%ingo%' AND Author.email LIKE '%wemove%')
 $authorTpl1 = &$query->getObjectTemplate('Author');
 $authorTpl1->setValue("name", "ingo", DATATYPE_ATTRIBUTE);
 $authorTpl1->setValue("email", "LIKE '%wemove%'", DATATYPE_ATTRIBUTE);

 // OR Author.name LIKE '%herwig%'
 $authorTpl2 = &$query->getObjectTemplate('Author', QUERYOP_OR);
 $authorTpl2->setValue("name", "herwig", DATATYPE_ATTRIBUTE);

 // Recipe.created >= '01.01.2004' AND Recipe.created < '01.01.2005'
 $recipeTpl1 = &$query->getObjectTemplate('Recipe');
 $recipeTpl1->setValue("created", ">= '01.01.2004'", DATATYPE_ATTRIBUTE);
 $recipeTpl2 = &$query->getObjectTemplate('Recipe');
 $recipeTpl2->setValue("created", "< '01.01.2005'", DATATYPE_ATTRIBUTE);
 
 // 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", DATATYPE_ATTRIBUTE);
 $recipeTpl4 = &$query->getObjectTemplate('Recipe');
 $recipeTpl4->setValue("portions", "= 4", DATATYPE_ATTRIBUTE);
 $query->makeGroup(array(&$recipeTpl3, &$recipeTpl4), QUERYOP_AND, QUERYOP_OR);

 $authorTpl1->addChild($recipeTpl1);
 $authorTpl1->addChild($recipeTpl2);
 $authorTpl1->addChild($recipeTpl3);
 $authorTpl1->addChild($recipeTpl4);
 $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 NodeUnifiedRDBMapper 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

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in 0.0338 seconds