Skip to content
flack edited this page Sep 26, 2019 · 8 revisions

The Database Abstraction Layer (DBA) is a wrapper around MgdSchema objects which provides MidCOM functionality to Midgard objects. Internally, this is done by the midcom_core_dbaobject baseclass that acts as a decorator.

Another important part of this layer is midcom_core_querybuilder, which provides query access to the storage backend.

Main Features

DBA classes must extend midcom_core_dbaobject, which provides methods like new_query_builder() and integrations with ACL and other Services.

There are also a number of event handlers 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:

public function _on_deleting()
{
   //your code here
   return $proceed //false cancels the operation, true causes it to proceed
}

public function _on_deleted()
{
   //your code here.
}

Apart from these methods, the more generic MidCOM Watches allow a component 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 Custom Object Schema

  1. create mgdschema.xml
  2. run composer run-script post-install-cmd (this creates a symlink to the project's var dir)
  3. run vendor/bin/midgard-portable schema to get the SQL imported and then metadata etc columns added
  4. in manifest.inc add
'class_mapping' => [
    'my_mgdschema_classname' => 'my_midcom_dba_classname'
],
  1. in component root create myclass.php, below is a basic example based on the definition above:
class my_midcom_dba_classname extends midcom_core_dbaobject
{
    public $__midcom_class_name__ = __CLASS__;
    public $__mgdschema_class_name__ = 'my_mgdschema_classname';
}
  1. Call /midcom-cache-invalidate to reload the manifest and DBA classes cache.
  2. Create new topic to test your new component.

Weblinks

http://www.nathan-syntronics.de/midgard/midcom/midcom-2_6/db-api.html (historical version)

Clone this wiki locally