Piotras' blog: Archive
2005-05-01 - 2005-05-31
Extend extended
Posted on 2005-05-10 18:11:54 UTC.
Only code:
class Amerigard extends NewMidgardArticle
{
}
class FlyHigh extends Amerigard
{
}
$object = new FlyHigh();
print_r($object);
$object->getByID(17);
output:
flyhigh Object
(
[sitegroup] =>
[author] =>
[owner] =>
[realm] =>
[guid] =>
[changed] =>
[updated] =>
[action] =>
[errno] =>
[errstr] =>
[id] =>
[calstart] =>
[caldays] =>
[icon] =>
[view] =>
[print] =>
[extra1] =>
[extra2] =>
[extra3] =>
[name] =>
[score] =>
[revised] =>
[revision] =>
[created] =>
[approved] =>
[locked] =>
[type] =>
[creator] =>
[revisor] =>
[approver] =>
[locker] =>
[up] =>
[topic] =>
[title] =>
[abstract] =>
[content] =>
[url] =>
[lang] =>
[sid] =>
[contentauthor] =>
)
midgard-lib:5071:debug:query=SELECT sitegroup, realm, guid, action, author
FROM repligard WHERE (realm='article' and id=17) AND (repligard.sitegroup in (0,0))
midgard-lib:5071:debug:query=SELECT up,locked,revision,extra3,view,extra2,print,topic,
name,extra1,approver,revised,creator,type,icon,calstart,approved,caldays,locker,score,
revisor,created,id FROM article WHERE article.id=17 AND (article.sitegroup in (0, 0)
OR 0<>0)
Follow this link , if you really do not understand this code.
Query Builder in action
Posted on 2005-05-19 16:10:58 UTC.
Thanks to Jukka's efforts, we have already working MidgardQueryBuilder.
Let's start with simple example.
/* Define which MgdSchema type should be used and returned by QB */MySQL query executed:
$qb = new midgardquerybuilder("NewMidgardArticle");
/* Define constraints */
$qb->addConstraint("topic", "<", 2);
$qb->addConstraint("title", "=", "News");
/* Execute SQL query and return array*/
$f = $qb->execute();
SELECT article.id FROM article_i,article
WHERE
article.topic < 2 AND article_i.title = 'News'
AND article.id=article_i.sid
As you notice, title property is defined in article_i table while topic property is defined in article table.
Query Builder follows class' tables definition and is able to search for objects which has more than one table as storage.
$qb->execute(); returned array with only one object ( due to record returned by SELECT ), so
print_r($f[0]);
etc etc etc
NewMidgardArticle Object
(
[sitegroup] => 0
[author] => 0
[owner] => 0
[realm] => article
[guid] => cedda8cb461c9f846c73f043aaf888e9
[changed] =>
[updated] =>
[action] => create
[errno] => 0
[errstr] =>
[id] => 28
[calstart] => 0000-00-00
Let's try to use datetime fields:
$qb = new midgardquerybuilder("NewMidgardArticle");
$qb->addConstraint("revised", ">", "2003-04-30 09:46:00");
$f = $qb->execute();MySQL query executed:SELECT article.id FROM article_i,article
WHERE
article.revised > '2003-04-30 09:46:00'
AND article.id=article_i.sid
Now $qb->execute() returned array with 5 objects. I do not want to print'em all , so let's look at revised properties
if were selected correctly:
print_r($f);
Array(
[0] => NewMidgardArticle Object
(
[revised] => 2003-04-30 10:30:06
[1] => NewMidgardArticle Object
(
[revised] => 2003-04-30 10:01:18
[2] => NewMidgardArticle Object
(
[revised] => 2003-04-30 11:03:31
[3] => NewMidgardArticle Object
(
[revised] => 2005-04-05 16:29:16
[4] => NewMidgardArticle Object
(
[revised] => 2005-05-12 12:36:18
Simple , fast and usefull :)
OK, now try to read about classes which extend MgdSchema classes and think how this could be used with Query Builder. PHP classes names are not case-sensitive, and MgdSchema type's names are. So if we could use only lowercases for type and classes names in MgdSchema we could extend MgdSchema classes and use own classes and objects with Query Builder too.
Just like this:
class Amerigard extends NewMidgardArticle
{
}
class FlyHigh extends Amerigard
{
}
$qb = new midgardquerybuilder("FlyHigh");
$qb->addConstraint("topic", "<", 2);
$qb->addConstraint("title", "=", "News");
$f = $qb->execute();MySQL query executed:SELECT article.id FROM
article_i,article
WHERE
article.topic < 2
AND article_i.title = 'News'
AND article.id=article_i.sid
Above example is not working example of course , but could be :)