Something you should know about metadata.

Posted on 2005-07-20 19:07:27 EEST.

Metadata definition for a good start.

It's good ( and even very good ) that new MgdSchema objects have new methods and midgard-php have new functions which describe classes' and objects' data ( like property type ) , and internal classes' data like: "which property keeps data ( id or guid ) which points directly to object's parent object in midgard tree". Writing code without such knowledge is sometimes like writing code with closed eyes.

I was a bit confused when I decided to use exactly the same enum type data in core and in midgard-php. Why? Because making iteration for enum is "easy as hell". At last I found solution in Alexander's repligard code , which let me create simple GSList and use its position as php integer value and its data as name to register constant.

So , new midgard-php  constants are already usable.

MGD_META_PROPERTY_PARENT
( use it when you want to know which property ( it's name ) points to parent object in midgard tree )
Schema definition: <property name="topic" type="integer" parentfield="topic"/>

MGD_META_PROPERTY_UP
( use it when you want to know which property ( it's name ) points to up object in midgard tree, usually object of the same type )
Schema definition: <property name="up" type="integer" upfield="up"/>

MGD_META_PROPERTY_PRIMARY
( use it when you want to know which property ( it's name ) is used as primary field ( id or guid ))
Schema definition: <property name="id" type="integer" primaryfield="id"/>

MGD_META_TREE_PARENT
( use it when you want to know the name of parent object's in midgard tree )
Schema definition: <type name="NewMidgardArticle" parent="NewMidgardTopic">

MGD_META_TREE_CHILDS
( use it when you want to know how many child ( in midgard tree ) objects ( and their names ) may be used for object )
There is no schema definition for this. Child types are created automagically when schema is loaded.


OK , let's look at some sample code. These constants may be used as function's or object method's parameters.

mgd_get_class_data( int metatype, string class name);
returns property name or array if found or false on failure

Yes, I am not happy with function which returns string or array , but you always know what you want to get while
writing code.

$parent = mgd_get_class_data(MGD_META_PROPERTY_PARENT , "NewMidgardArticle");

/* or, if you would like to use object :
$art = new NewMidgardArticle();
$parent = $art->get_data(MGD_META_PROPERTY_PARENT);
*/

print $parent;

output:

topic

Another example:

$topic = new NewMidgardTopic();
$childs =  $topic->get_data (MGD_META_TREE_CHILDS);
print_r($childs);
output  :
Array
(
    [NewMidgardArticle] =>
    [NewMidgardArt] =>
    [PPNewMidgardArt] =>
    [org_openpsa_document] =>
)

OK. What about properties metadata?

mgd_get_class_var(string property name, string class name);
returns array with property name as key


$name = mgd_get_class_var("name", "NewMidgardTopic");

/* or
$topic = new NewMidgardTopic();
$name = $topic->get_var("name");
*/
print_r($name);
output:

Array
(
    [name] => Array
        (
            [type] => string
        )
)

mgd_get_class_property is an alias to mgd_get_class_var.


mgd_get_class_vars(string class name);
returns array with all class default properties and their types

$props  = mgd_get_class_vars("NewMidgardTopic");
output:

[...]
   [revision] => Array
        (
            [type] => int
        )
    [created] => Array
        (
            [type] => string
        )
    [revisor] => Array
        (
            [type] => int
        )
etc etc etc

mgd_get_class_properties function is an alias to mgd_get_class_vars.



Back

Layout Copyright © 2006 Finnish Teleservice Center Ltd Oy - Site Powered by Midgard CMS