PHPackages                             gajus/moa - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. gajus/moa

ActiveLibrary[Database &amp; ORM](/categories/database)

gajus/moa
=========

MOA implements dynamically generated Active Record database abstraction.

0.1.2(12y ago)49262[12 issues](https://github.com/gajus/moa/issues)[1 PRs](https://github.com/gajus/moa/pulls)BSD-3-ClausePHPPHP &gt;=5.4

Since Apr 25Pushed 11y ago10 watchersCompare

[ Source](https://github.com/gajus/moa)[ Packagist](https://packagist.org/packages/gajus/moa)[ Docs](https://github.com/gajus/moa)[ RSS](/packages/gajus-moa/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (2)Versions (3)Used By (0)

MOA
===

[](#moa)

[![Build Status](https://camo.githubusercontent.com/1f7987c6c8c6435e09c1f5b6ac34e6ab48dafea05f6200b1cf2fd3c7a5bbf7ba/68747470733a2f2f7472617669732d63692e6f72672f67616a75732f6d6f612e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/gajus/moa)[![Coverage Status](https://camo.githubusercontent.com/cb324f79a3d7b0d5a064e22ff057c6963c9d5497a9fe5e12c155d295db8b599e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f67616a75732f6d6f612f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/gajus/moa?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/8e56deabb17f76a9783171eebbb148fee19d2edb248f8b1a8c3add596c4e97d5/68747470733a2f2f706f7365722e707567782e6f72672f67616a75732f6d6f612f76657273696f6e2e706e67)](https://packagist.org/packages/gajus/moa)[![License](https://camo.githubusercontent.com/7a50fb1d6a394749aa8e43285906e9ef6e3746c80d46c72e48c49219eb68cfef/68747470733a2f2f706f7365722e707567782e6f72672f67616a75732f6d6f612f6c6963656e73652e706e67)](https://packagist.org/packages/gajus/moa)

> This project is no longer maintained.

MOA (Mother of All) is a database abstraction using [Active Record](http://en.wikipedia.org/wiki/Active_record_pattern) pattern:

> Active record is an approach to accessing data in a database. A database table or view is wrapped into a class. Thus, an object instance is tied to a single row in the table. After creation of an object, a new row is added to the table upon save. Any object loaded gets its information from the database. When an object is updated the corresponding row in the table is also updated. The wrapper class implements accessor methods or properties for each column in the table or view.

– [http://en.wikipedia.org/wiki/Active\_record\_pattern](http://en.wikipedia.org/wiki/Active_record_pattern)

MOA is designed to handle [CRUD](http://en.wikipedia.org/wiki/Create,_read,_update_and_delete) operations.

MOA is not [ORM](http://en.wikipedia.org/wiki/Object-relational_mapping). MOA does not work with object relations and dependencies. However, these libraries do:

- [Doctrine](http://www.doctrine-project.org/)
- [Propel](http://propelorm.org/)

MOA does not implement elaborate finders, filters or methods for querying data. However, these libraries do:

- [PHP ActiveRecord](https://github.com/jpfuentes2/php-activerecord)
- [Paris](https://github.com/j4mie/paris)
- [Parm](https://github.com/cassell/Parm)

Hierarchy &amp; Responsibilities
--------------------------------

[](#hierarchy--responsibilities)

### Builder

[](#builder)

MOA is using dynamic code generation to represent your database. [builder script](#builder-script) generates a file for each table using attributes fetched from the database (e.g. column name, type, default value, etc.). These classes are generated dynamically to reduce the amount of hand-coded duplication of the data representation.

This is an [example of generated class](#api).

With other Active Record implementations you do not need a generator because these properties are either hand-typed or fetched during the execution of the code. The former is tedious and error-prone, while the latter is a lazy-workaround that has a considerable performance hit.

### Mother

[](#mother)

All models extend `Gajus\MOA\Mother`. Mother attempts to reduce the number of executions that would otherwise cause an error only at the time of interacting with the database. This is achieved by using the pre-fetched table attributes to work out when:

- Accessing a non-existing property.
- Setting property that does not pass derived or custom validation logic.
- Saving object without all the required properties.

Furthermore, Mother is keeping track of all the changes made to the object instance. `UPDATE` query will include only the properties that have changed since the last synchronization. If object is saved without changes, then `UPDATE` query is not executed.

> If you know a negative downside of the behavior described above, please [contribute](https://github.com/gajus/moa/issues/1) a warning.

Delete operation will remove the object reference from the database and unset the primary key property value.

### Hierarchy

[](#hierarchy)

Using MOA you can [choose your own namespace](#builder-script)) and have your own [base class](#mother-1).

This is an example of an application hierarchy incorporating all of the MOA components:

```
Gajus\MOA\Mother
    you base model [optional]
        MOA generated models
            your hand-typed models [optional]
                your hand-typed domain logic [optional]

```

API
---

[](#api)

This section of the documentation is using code examples from a fictional application to introduce you to the API. The `My\App\Model\Person` model in the example extends from:

```
/**
 * This class is generated using https://github.com/gajus/moa.
 * Do not edit this file; it will be overwritten.
 */
abstract class Person extends \Gajus\MOA\Mother {
    const TABLE_NAME = 'person';
    const PRIMARY_KEY_NAME = 'id';

    static protected
        $columns = [
            'id' => [
                'column_type' => 'int(10) unsigned',
                'column_key' => 'PRI',
                'column_default' => NULL,
                'data_type' => 'int',
                'is_nullable' => false,
                'extra' => 'auto_increment',
                'character_maximum_length' => NULL,
            ],
            'name' => [
                'column_type' => 'varchar(100)',
                'column_key' => '',
                'column_default' => '',
                'data_type' => 'varchar',
                'is_nullable' => false,
                'extra' => '',
                'character_maximum_length' => 100,
            ],
            'language' => [
                'column_type' => 'varchar(100)',
                'column_key' => '',
                'column_default' => 'English',
                'data_type' => 'varchar',
                'is_nullable' => false,
                'extra' => '',
                'character_maximum_length' => 100,
            ]
        ];
}
```

### Create and Update

[](#create-and-update)

Object is inserted and updated using `save` method. Object is inserted to the database if instance primary key property has no value. Otherwise, object is updated using the primary key property value.

```
/**
 * @param PDO $db
 * @param int $id
 */
$person = new \My\App\Model\Person($db);

// Set property
$person['name'] = 'Foo';

// Insert object to the database
$person->save();
# $person['id'] 1
```

When object is inserted to the database, new object state is fetched from the database:

```
// Note that "language" property was not set,
// though it had default value in the table schema.
# $person['language'] English

// Update property
$person['name'] = 'Bar';

// Save object state to the database
$person->save();
# $person['id'] 1
```

### Delete Object

[](#delete-object)

Deleting object will remove the associated entry from the database and unset the primary key property value.

```
$person->delete();
# $person['id'] null
```

However, other property values are not discarded. If the same object instance is saved again, it will be inserted to the database with new primary key value:

```
# $person['name'] Bar
$person->save();
# $person['id'] 2
```

### Inflate Object

[](#inflate-object)

Object is inflated using the primary key:

```
$person = new \My\App\Model\Person($db, 2);
```

In the above example, object data is retrieved from the database where primary key value is "2".

### Getters and Setters

[](#getters-and-setters)

MOA implements `ArrayAccess` interface. You can manipulate object properties using the array syntax, e.g.

```
