Open Source Content Management System

MidgardQueryBuilder Objects and Parameters

  1. Query object with one parameter.
  2. Query object with more than one parameter's constraint.

midgard_query_builder class is very usefull to fetch object's data from database, but it's a bit limited if you need to query object and its parameters.

Basically, during your development work, you have two cases to fetch object specifing parameters constraints. The first case is the simplest, when you need to query object and only one parameter's constraint should be used. The second is much more difficult, when you need to query object and more than one parameter's constraint should be defined. In this case workaround is needed.

Those two examples should explain how to write code for both cases:

Query object with one parameter.

$qb = new midgard_query_builder("midgard_article");
$qb->add_constraint("name", "=", "news");
$qb->add_constraint("parameter.value", "=", "cool"); 
$ret = $qb->execute();

$my_object =& $ret0

Query object with more than one parameter's constraint.

$qb = new midgard_query_builder("midgard_parameter");
$qb->add_constraint("domain", "=", "midcom");
$qb->add_constraint("name", "=", "component");
$qb->add_constraint("value", "=", "my_component");
$ret = $qb->execute();

$my_object = new midgard_topic($ret0>parentguid);

Note that we initialized QB instance for midgard_parameter, not for objec's class we wanted to fetch. This cases requires two queries instead of one, but unfortunatelly midgard_query_builder is not aware ( and can not guess ) if you defined constraint in the same table's scope or another one ( which might be tha same ) for which SQL join should be made.

In latter example, one object has been instatiated. But of course , you may need less constraints if you need fetch more objects:

$qb = new midgard_query_builder("midgard_parameter");
$qb->add_constraint("domain", "=", "midcom");
$ret = $qb->execute();

foreach($ret as $obj) {
    $my_object = new midgard_topic($obj->parentguid);
    echo "Found {$my_object->name}";
}
Designed by Nemein, hosted by Anykey