2017 © Pedro Peláez
 

library midgard-portable

ActiveRecord ORM built on top of Doctrine 2

image

openpsa/midgard-portable

ActiveRecord ORM built on top of Doctrine 2

  • Friday, July 27, 2018
  • by flack
  • Repository
  • 3 Watchers
  • 5 Stars
  • 9,963 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 17 Versions
  • 5 % Grown

The README.md

midgard-portable Code Coverage

This library provides an ActiveRecord ORM built on top of Doctrine 2 and is modeled after the Midgard API., (*1)

In a Nutshell

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

<type name="my_person" table="person">
    <property name="id" type="unsigned integer" primaryfield="id">
        <description>Local database identifier</description>
    </property>
    <property name="firstname" type="string" index="yes">
        <description>First name of the person</description>
    </property>
    <property name="lastname" type="string" index="yes">
        <description>Last name of the person</description>
    </property>
</type>

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:, (*3)

// 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:, (*4)

$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:, (*5)

$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:, (*6)

$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., (*7)

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)., (*8)

Usage

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

<?php
use midgard\portable\driver;
use midgard\portable\storage\connection;

$db_config = [
    'driver' => 'pdo_sqlite',
    'memory' => true
];
$schema_dirs = ['/path/to/my/schemas/'];
$var_dir = '/path/to/vardir';
$entity_namespace = '';
$dev_mode = false;

$driver = new driver($schema_dirs, $var_dir, $entity_namespace);
connection::initialize($driver, $db_config, $dev_mode);

Change the parameters as required. After calling connection::initialize(), you can interact with the database through Midgard API as outlined above., (*10)

CLI tools

midgard-portable needs to generate entity classes as well as ClassMetadata and Proxy classes for Doctrine. In development setups, this is done automatically on each request. For production installations, you can run the following CLI command:, (*11)

./bin/vendor/midgard-portable schema

It works very much like the midgard-schema tool of old, i.e. it will generate midgard_object classes based on MgdSchema XML files, the accompanying mapping data and proxy classes, and also create/update the corresponding database tables. You will need to run this once during initial installation, and then again each time the MgdSchemas change., (*12)

You can also use Doctrine's CLI runner and all the functionality it provides if you create a file under the name cli-config.php, with this content:, (*13)

<?php
use midgard\portable\storage\connection;
require 'my_settings_file.php'; //This needs to contain the code shown above
$entityManager = connection::get_em();

The Versions

27/07 2018

dev-master

9999999-dev

ActiveRecord ORM built on top of Doctrine 2

  Sources   Download

LGPL LGPL-2.1-or-later

The Requires

 

28/05 2017

v1.0.0

1.0.0.0

ActiveRecord ORM built on top of Doctrine 2

  Sources   Download

LGPL

The Requires

 

30/12 2016

v0.9.2

0.9.2.0

ActiveRecord ORM built on top of Doctrine 2

  Sources   Download

LGPL

The Requires

 

18/12 2016

v0.9.1

0.9.1.0

ActiveRecord ORM built on top of Doctrine 2

  Sources   Download

LGPL

The Requires

 

15/12 2016

v0.9.0

0.9.0.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires

 

16/11 2016

v0.8.4

0.8.4.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires

 

01/11 2016

v0.8.3

0.8.3.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires

 

26/06 2016

v0.8.2

0.8.2.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires

 

11/10 2015

v0.8.1

0.8.1.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires

 

26/09 2015

v0.8.0

0.8.0.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires

 

23/12 2014

v0.7.0

0.7.0.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires

 

05/09 2014

v0.6.0

0.6.0.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires

 

01/07 2014

v0.5.0

0.5.0.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires

 

22/05 2014

v0.4.0

0.4.0.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires

 

20/04 2014

v0.3.0

0.3.0.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires

 

29/12 2013

v0.2.0

0.2.0.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires

 

11/11 2013

v0.1.0

0.1.0.0

Pure PHP implementation of (parts of) the Midgard2 API

  Sources   Download

LGPL

The Requires