New MgdSchema methods
Posted on 2005-07-12 20:34:02 EEST.
I have been thinking about simple and fast list method , which could list "child" objects in tree. It was quite fast to implement midgard_object_list function and $object->list() method for midgard-php. Ah! Too fast. Someone could try to set order using such method. So, what could be the best choice? Midgard Query Builder :)
There are already two similiar methods for new midgard-php objects registered with MgdSchema.
- list() List all objects of the same type which have upfield set to current object's primary field.
- list_childs() List all objects of "child" type which have parentfield defined.
Both method returns new MidgardQueryBuilder object instance with parent or up property already being added ( transparently in core ) as constraint to Midgard Query Builder. Here's some simple example from the code I used for tetsing:$sn = new NewMidgardSnippetdir();
$sn->get_by_id(186);
$qb = $sn->list();
$objects = $qb->execute();
$parent = $objects[0]->get_parent();
Example looks a bit difficult and seems to be too much complicated for such trivial thing which was always made using mgd_list_xxxx functions. Yes, it is , but it is quite good ( perfect ? ;) way to write additional wrappers and make any API which let's you limit objects retrieved from database.
get_parent method returns "parent" ( in tree ) object which is defined with parent and parenfield attributes in midgard schema.
I also added ( interesting IMO ) get method. This is simple method which returns only one object and searches for the object comparing property values directly. Few months ago I wrote about find() method which could be used as a replacement for mgd_get_xxx_by_name, but get is even better.
$article = new NewMidgardArticle();
$article->topic = 1;
$article->name = "index";
$article->get();
No fetchables , no arrays in a place when you expect only one object.
What is also interesting is get_by_path method , which "returns" object if found.
$article = new NewMidgardArticle();
$article->get_by_path(""/192-168-0-17-pptest-root-topic/test/img_0016");
I didn't make many tests , but this method should work without any problems for trees like this: /topic/topic/article/article/article.
I wanted to make get_by_path method asap , cause tarjei is writing new midgard webdav class. Using desktop apps with midgard will be much more than cool pleasure :)