Query Builder

flack edited this page Dec 8, 2016 · 4 revisions

Query Builder is a feature in the Midgard which is used to run database queries. It only works with data structures based on MgdSchema.

In MidCOM, Query Builder functionality is available to [DBA classes](MidCOM DBA) in the new_query_builder() method, while the class midcom_helper__dbfactory (accessible via the midcom accessor) allows for generic access.

Query Builder always returns MgdSchema objects. If only specific bits of information are needed, Midgard Collector can be used instead.

Aside from attributes, Midgard Metadata, Attachment and Parameter constraints can be used when querying objects.


// Instantiate Query Builder for an article object
$qb = new midgard_query_builder('midgard_article');

// List articles only in topic 123
$qb->add_constraint('topic', '=', 123);

// Run the query
$articles = $qb->execute();

// Show the articles
foreach ($articles as $article)
    echo $article->title . "<br />\n";