Fork me on GitHub

Midgard2 PHPCR

Standard PHP Content Repository implementation

A Midgard2 -backed provider of the PHP Content Repository (PHPCR) interfaces. phpcr-midgard2 is a Symfony CMF compatible PHPCR provider that can be used in PHP Content Management Systems without requiring Java.

Using Midgard2 instead of Apache Jackrabbit also has the benefit of making interoperability with regular relational databases used by many CMSs easy. Midgard2 supports multiple databases, including MySQL, Postgres, and SQLite.

Each node type gets its own table, with the tree structure managed separately in the database. This makes it easy to map existing relational databases into the PHPCR tree.



You need to have a midgard2 PHP extension installed. On many distributions setting this up is as simple as:

$ sudo apt-get install php5-midgard2

If your distribution doesn't come with Midgard2, then you can either compile it manually or use our precompiled packages.

Then set your project to depend on midgard/phpcr by having your composer.json to include:

"require": {
    "midgard/phpcr": ">=1.0"

Then just install the provider via Composer:

$ wget
$ php composer.phar install

You also need to copy the Midgard2 PHPCR schemas from vendor/midgard/phpcr/data/share to your schema directory (by default /usr/share/midgard2):

$ sudo cp vendor/midgard/phpcr/data/share/schema/* /usr/share/midgard2/schema/
$ sudo cp vendor/midgard/phpcr/data/share/views/* /usr/share/midgard2/views/

Getting started

You can use the Composer-generated autoloader to load all needed classes:

require 'vendor/autoload.php';

After you've included the autoloader you should be able to open a Midgard2 repository session:

// Set up Midgard2 connection
$parameters = array(
    // Use local SQLite file for storage
    'midgard2.configuration.db.type' => 'SQLite',
    '' => 'midgard2cr',
    'midgard2.configuration.db.dir' => __DIR__,
    // Where you want to store file attachments
    'midgard2.configuration.blobdir' => '/var/lib/midgard2/blobs',
    // Let Midgard2 initialize the DB as needed
    'midgard2.configuration.db.init' => true,

// Get a Midgard repository
$repository = Midgard\PHPCR\RepositoryFactory::getRepository($parameters);

// Log in to get a session
$credentials = new \PHPCR\SimpleCredentials('admin', 'password');
$session = $repository->login($credentials, 'default');

After this the whole PHPCR API will be available. See some example code in the examples directory.

With MySQL, the connection parameters could for example be:

$parameters = array(
    // MySQL connection settings. The database has to exist
    'midgard2.connection.db.type' => 'MySQL',
    '' => 'midgard2',
    'midgard2.connection.db.username' => 'midgard',
    'midgard2.connection.db.password' => 'midgard',
    '' => '',
    'midgard2.connection.db.port' => '3306'
    // Let Midgard2 initialize the DB as needed
    'midgard2.configuration.db.init' => true,

This is the only different part from the example using SQLite above.

How does PHPCR map to your database?

Midgard2 uses standard relational databases for content storage. The PHPCR model is mapped to database tables in the following way:

  • Tree structure is defined in midgard_node table, which contains name, parent reference, and a reference to a content object
  • Properties of a node type are stored in the table defined for that node type in its MgdSchema (see examples)
  • Multivalued properties, and properties not defined in the node type (for example with nt:unstructured) are stored in the midgard_node_property table, which contains name, reference to midgard_node entry, type and value
  • Binary property contents are stored as files into the blobdir defined in repository configuration

New Node Types can be registered by writing MgdSchemas for them and copying them to Midgard's schema directory (by default /usr/share/midgard2/schema).

Using with Doctrine ODM

The Midgard2 PHPCR provider can be used with the Doctrine PHPCR ODM. To start using it, prepare a Midgard2 PHPCR session as explained in the Getting started document, and then:

// Configuring a Midgard2 PHPCR session before this

// prepare the doctrine configuration
$config = new \Doctrine\ODM\PHPCR\Configuration();

// Create a Document Manager instance for the PHPCR session
$dm = \Doctrine\ODM\PHPCR\DocumentManager::create($session, $config);

// After this you can use PHPCR ODM normally, for example:

// fetching a document by PHPCR path (id in PHPCR ODM lingo)
$user = $dm->getRepository('Namespace\For\Document\User')->find('/bob');
//or let the odm find the document class for you
$user = $dm->find('/bob');

// create a new document
$newUser = new \Namespace\For\Document\User();
$newUser->username = 'Timmy';
$newUser->email = '';
$newUser->path = '/timmy';
// make the document manager know this document
// this will create the node in PHPCR but not read the 
// fields or commit the changes yet.

// store all changes, insertions, etc. with the storage backend

If you're using the Doctrine PHPCR Symfony Bundle, you can also configure PHPCR ODM through it.