MidCOM Database Abstraction Layer

From MidgardWiki

Jump to: navigation, search

MidCOM's Database Abstraction Layer (DBA) provides an interface to MgdSchema functionality of the Midgard Core. They are part of the MidCOM Base Classes an act like a wrapper and provide Midgard functionality to the MidCOM framework. Aside from this, there is also a MidCOM Legacy Database Abstraction Layer which provides legacy accesss to certain objects.

Internally, this is facilitated by a code generator which produces the necessary base classes from MgdSchema XML files. These have to be extended in the respective MidCOM Component to get a PHP object representing the database table.

Contents

Main Features

One important part of this layer is midcom_core_querybuilder, which provides access to the Midgard MySQL Database via Query Builder. In all DBA-based classes, methods like new_query_builder() and functionality like the MidCOM ACL system is automatically available.

There are also a number of "event handler" functions that are automatically triggered when a DBA object is updated, created or deleted. One of them is called before the operation, another one after it is completed. For deletion, they look like this:

function _on_deleting()
{
   //your code here
   //you can use $this to operate on the object in question
   return $proceed //false cancels the operation, true causes it to proceed
}
 
function _on_deleted()
{
   //your code here. 
   //you can use $this to operate on the object in question
}

Apart from these methods, the more generic MidCOM Watches allow a Component (or the MidCOM core) to react to changes done to other objects as well, for example a Midgard Replicator run can be triggered when an object is updated or deleted.

Example

If you have a database table mytable consisting of two columns, mystring and myfloat, and a correctly defined Mgdschema file to go along, the DBA system will provide you with a PHP object with both columns as properties and all the standard methods, so code like this is possible:

//create a new entry in the database:
$object = new mytable_dba_class();
$object->mystring = "something";
$object->myfloat = 2.3456;
$object->create();
 
//retrieve entries from the db:
$qb = mytable_dba_class::new_query_builder();
$qb->add_constraint('myfloat', '>', 3);
$qb->add_order('mystring');
$entries = $qb->execute();

Creating a Component with a Custom Object Schema

A small howto from the midgard-users forum:

  1. create component skeleton using for example the scaffold
  2. create mgdschema.xml
  3. symlink the .xml from MIDGARD_PREFIX/share/midgard/schema/ using the component name as filename.
  4. stop apache
  5. run midgard-schema database_name to get the SQL imported and then metadata etc columns added
  6. start apache
  7. in manifest.inc add 'class_definitions' => Array('midcom_dba_classes.inc'),
  8. edit/create file midcom_dba_classes.inc (in the config directory. See the following example of a class
array(
    'table' => 'org_routamc_positioning_location',
    'old_class_name' => null,
    'new_class_name' => 'org_routamc_positioning_location',
    'midcom_class_name' => 'org_routamc_positioning_location_dba'
),
  1. in component root create my_class.php, below is a basic example based on the definition above:
class org_routamc_positioning_location_dba extends __org_routamc_positioning_location_dba
{
    function org_routamc_positioning_location_dba($id = null)
    {
        return parent::__org_routamc_positioning_location_dba($id);
    }
}
  1. Call /midcom-cache-invalidate to reload the manifest and DBA classes cache.
  2. Create new topic to test your new component.

Classes

At the moment, API documentation for the DBA classes cannot be generated for DBA classes, you'll find them at the second and the third link.

midcom_baseclasses_database_article
midcom_baseclasses_database_attachment
midcom_baseclasses_database_element
midcom_baseclasses_database_event
midcom_baseclasses_database_eventmember
midcom_baseclasses_database_group
midcom_baseclasses_database_host
midcom_baseclasses_database_member
midcom_baseclasses_database_page
midcom_baseclasses_database_pageelement
midcom_baseclasses_database_person
midcom_baseclasses_database_snippet
midcom_baseclasses_database_snippetdir
midcom_baseclasses_database_style
midcom_baseclasses_database_topic

Weblinks

http://bergie.iki.fi/blog/introduction_to_midgards_database_abstraction_system.html
http://www.midgard-project.org/documentation/midcom-dba/
http://www.midgard-project.org/documentation/midcom-dba-object-api/
http://www.nathan-syntronics.de/midgard/midcom/midcom-2_6/db-api.html
Personal tools