Database - dknx01/micromvc GitHub Wiki

Database

The database is initialised during the bootstrap, if it is not deactivated in the configuration file.

All databases model are stored in Application/Db.

For each Table you need the following three files:

  • Table.php
  • Model.php
  • Mapper.php

The Table

Extends \Mvc\Db\Table and you only need to set the table name:

protected $name = 'test';

The Model

Extends \Mvc\Db\Model and represents the internal model for the table.

The Mapper

Extends \Mvc\Db\Mapper and maps the database table structur to the model structure. And it should set the primary key for the table.

public function __construct()
    {
        $mapper = array(
            'id' => 'id',
            'name' => 'testName'
        );
        $this->setMapper($mapper)
             ->getPrimary('id');
    }

The ResultIterator

Normally a database query returns all rows as a collection for models, one for each row. To use this collection it is wrapped in an interator, the ResultIterator.

For usage see the php.net iterator help. It is more or less a normal iterator.

Query

For normal select operations there is an own object. \Mvc\Db\Query\Query_Select

Usage:

$table = an table instance;
$select = new \Mvc\Db\Query_Select($table))
$select->column('COLUMNNAME as ALIAS')
       ->where(CONDITION)
       ->limit(LIMIT)
       ->query();