MidgardQueryBuilder Complex Constraints
A special dot '.' separator may be used for add_constraint and add_order MidgardQueryBuilder methods. The main purpose of the dot separator is to query metadata and referenced types defined as properties' link in the MgdSchema files.
The basic syntax for constraints and dot separator is:
propertyA.propertyB
propertyA is a member of a class which was initialized with Midgard Query Builder. propertyB is a member of a class which was defined as "linked" one in Midgard schema file.
There are few exceptions ( reserved words ) for propertyA name :
metadata
add_constraint("metadata.created", ">" , "2006-01-27 00:00:00");
parameter
add_constraint("parameter.name", "=" , "component");
attachment
add_constraint("attachment.mimetype", "LIKE" , "jpg");
Metadata query example
An example for $article->name and $article->metadata->created constraints:
<?php
$qb = new MidgardQueryBuilder("midgard_article");
$qb->add_constraint("metadata.created", ">" , "2006-01-27 00:00:00");
$qb->add_constraint("name", "=", "index");
$returned_array = $qb->execute();
?>
Only those objects will be queried which name is 'index' and were created after January 27th, 2006.
Referenced type query example
Schema type example for referenced types.
<type name="midgard_article" table="article">
<property name="creator" link="midgard_person:id"/>
</type>
PHP example
<?php
$qb = new MidgardQueryBuilder("midgard_article");
$qb->add_constraint("creator.username", "LIKE" , "%john%");
$qb->add_constraint("name", "=", "index");
$returned_array = $qb->execute();
?>
Only those objects will be queried which name is 'index' and referenced object's username property contains 'john'.
Mixed complex query example
Schema definition:
<type name="midgard_member" table="member" parentfield="">
<property name="id" type="integer" primaryfield="id"/>
<property name="extra" type="string"/>
<property name="uid" type="integer" link="midgard_person:id"/>
<property name="gid" type="integer" link="midgard_group:id" />
</type>
<?php
$qb = new midgardquerybuilder("midgard_member");
$qb->add_constraint('gid.name', '=', "Users group");
$qb->add_constraint('metadata.created', '>', "2006-02-27 18:45:00");
$qb->add_order('uid.firstname');
$returned_array = $qb->execute();
?>
Such query returns objects for those persons who are members of "User group" group, and were created after 27th February, 2006. Returned array is sorted by firstname of person record.
