MgdSchema file properties
- Defining Custom Types
- Defining Properties
- Settting the Property Type
- Specifying the Database Data Type
- Resolving Property Name and Table Field Conflicts.
- Setting the Primary Key
- Defining Tree Hierarchies
- References to Other Types
- Including Other Schemas
- Creating Types with Multilingual Content
MgdSchema files use XML attributes and properties.
Some type and table names are reserved since Midgard 1.8. See the list of reserved words.
Mandatory attributes are:
- type
- property
- name
Defining Custom Types
For every newly-defined type, mandatory attributes have to be set. To define a new MySuperClass type, use the type element:
<type name="MySuperClass">
</type>
This is sufficient to create a new type and initialize the corresponding new class when the schema is loaded. Such a type is accessible on the midgard-php level as the MySuperClass class.
Defining Storage Locations
Empty classes (let's say "interfaces" on midgard-php4 level) may be constructed without storage (i.e. a corresponding database table) defined. To set the table name for a particular type, the XML property table should be used:
<type name="MySuperClass" table="my_table">
</type>
Defining Properties
To define class members (object properties) for a new type, use property elements for child attributes:
<type name="MySuperClass" table="my_table">
<property name="title"/>
</type>
On the midgard-php, you can get or set the property value using the typical PHP syntax: $object->title.
Settting the Property Type
By default all properties are string type, unless another type is distinctly set. Available data types are:
- string
- integer
- unsigned integer (since 1.8)
- longtext
- float
- bool (bool or boolean since 1.8)
- datetime
- guid (since 1.8)
For every XML property data type there is a corresponding database data type :
- string is equal to varchar(255)
integer is equal to int(11)
Values can range from -2,147,483,648 to 2,147,483,647
unsigned integer is equal to int(11)
Values can range from 0 to 4,294,967,295
- longtext is equal to longtext ( or text )
- float is equal to float
- bool is equal to boolean
- guid is equal to varchar(80)
Specifying the Database Data Type
The additional XML property dbtype may be used when a specific database type is needed, for example, a property with the string type could use the varchar(80) type instead of varchar(255).
<property name="title" type="string" dbtype="varchar(80)"/>
or
<property name="info" type="string" dbtype="set('auth')"/>
Resolving Property Name and Table Field Conflicts.
The XML property field can be used if you want the object's property name to be different from the table's column name.
<property name="title" type="string" field="otherfield"/>
This describes that object's property 'title' will use the column 'otherfield' as its value storage. This is equal to SQL's 'SELECT table.otherfield AS title'.
Setting the Primary Key
When a type has many properties defined and one of those should be used as (or better, point to) the table's primary key, the XML property primaryfield can be used.
<property name="primary" type="integer" primaryfield="id"/>
unsigned integer
Defining Tree Hierarchies
MgdSchema is able to create tree hierarchies. Objects of the same type may be managed in a tree structure by using the XML property upfield. For defining a type as a 'child' object of another type, the XML properties parentfield and parent should be used.
- upfield is defined as property of an XML 'property' attribute
- parentfield is defined as property of an XML 'property' attribute
- parent is defined as property of an XML 'type' attribute
<type name="MyClass" parent="MySuperClass" table="childnodes">
<property name="parent" parentfield="superid" />
<property name="up" upfield="up" />
</type>
This definition describes the MyClass type, which is a node in the MySuperClass tree, and also may have own nodes with objects of the same (MyClass) Type. Useful methods for such types are documented in MgdSchema object's API (list_childs, is_in_tree, get_by_path).
References to Other Types
The link attribute's property should be used when the object's property (and the column's record value) holds a pointer to another type. The property's value should hold the primary property value of the referenced type.
<property name="creator" type="integer" link="midgard_person:id"/>
The special : separator describes which property of the referenced type should be used as the property's value. If this separator is not defined, the guid property of the referenced type is used by default.
See how to query referenced objects
Including Other Schemas
Applications may use as many schemas as needed. The include attribute can be used to load other schemas. There is no limitation to the number of included schemas. The full path to the schema file to be included can be freely defined.
<include name="/home/smith/midgard/schemas/favourite.xml"/>
Creating Types with Multilingual Content
To use multilingual features in a newly-created type, the properties multilang and table should be set.
<property name="content" type="longtext" multilang="yes" table="childnodes_i"/>
There is no need to define 'multilang' for every property when more than one property has multilingual content. This value is "inherited" and used for every XML property which has the same multilingual table set as value.
Multilang support in MgdSchema has a few limitations:
- defined table must be suffixed with '_i'
- property 'sid' must be defined as class member
