PHPackages                             openpsa/midgard-portable - 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. openpsa/midgard-portable

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

openpsa/midgard-portable
========================

ActiveRecord ORM built on top of Doctrine 2

v1.10.6(3mo ago)622.6k↓50%2[1 issues](https://github.com/flack/midgard-portable/issues)4LGPL-2.1-or-laterPHPPHP &gt;=8.1CI passing

Since Nov 11Pushed 3mo ago3 watchersCompare

[ Source](https://github.com/flack/midgard-portable)[ Packagist](https://packagist.org/packages/openpsa/midgard-portable)[ RSS](/packages/openpsa-midgard-portable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (41)Used By (4)

midgard-portable [![Code Coverage](https://camo.githubusercontent.com/76da09dc2fc9fe7c6e13ee0e6cd24ceee2620b4631a4241a2df19f3ce543b82c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f666c61636b2f6d6964676172642d706f727461626c652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/flack/midgard-portable/?branch=master)
=============================================================================================================================================================================================================================================================================================================================================================================

[](#midgard-portable-)

This library provides an ActiveRecord ORM built on top of Doctrine 2 and is modeled after the [Midgard](http://www.midgard-project.org) API.

In a Nutshell
-------------

[](#in-a-nutshell)

You can define your entities in XML (usually referred to MgdSchema):

```

        Local database identifier

        First name of the person

        Last name of the person

```

Running `midgard-portable schema` will create a corresponding database table and a PHP class (usually referred to as the MgdSchema class). You can use this to read from and write to the DB:

```
// create a new person
$person = new my_person();
$person->firstname = 'Alice';
if ($person->create()) {
    echo 'Created person #' . $person->id;
}
// load a new copy of the same person
$loaded = new my_person($person->id);
$loaded->firstname = 'Bob';
if ($loaded->update()) {
    echo 'Renamed from ' . $person->firstname . ' to ' . $loaded->firstname;
}
```

midgard-portable automatically adds metadata to the record:

```
$person = new my_person();
$person->firstname = 'Alice';
$person->create();
sleep(1);
$person->lastname = 'Cooper';
$person->update();
echo 'Person was created on ' . $person->metadata->created->format('Y-m-d H:i:s');
echo  ' and last updated on ' . $person->metadata->updated->format('Y-m-d H:i:s');
```

It also supports soft-delete:

```
$person = new my_person();
$person->firstname = 'Alice';
$person->create();
$person->delete();
try {
    $loaded = new my_person($person->id);
} catch (midgard_error_exception $e) {
    echo $e->getMessage(); // prints "Object does not exist."
}
// Revert the deletion
my_person::undelete($person->guid);
// or remove the entry completely
$person->purge();
```

You can query entries like this:

```
$qb = new midgard_query_builder('my_person');
$qb->add_constraint('metadata.created', '>', '2012-12-10 10:00:00');
$qb->add_order('firstname');
foreach ($qb->execute() as $result) {
    echo $result->lastname . "\n";
}
```

Or, you simply use Doctrine's builtin `QueryBuilder`.

Then, there's object trees, links, working with files, import/export of data and lots more, but until there is time to document all that, you'll have to read the source to find out (the unit tests might also be a good starting point).

Usage
-----

[](#usage)

To include `midgard-portable` in your application, simply `require` it in your `composer.json`. You can bootstrap the adapter like this:

```
