Doctrine PHPCR-ODM now handles versioning

I repost some of my blog posts made @ liip. Please see here for the original post and comments:

The Doctrine PHPCR-ODM allows you to easily map your PHP objects onto content repository nodes. Since last week, the PHPCR-ODM leverages the versioning support of PHPCR in the ODM layer. This gives your application a very simple way to work with versioned content.

The versioning is pretty simple to use. DocumentManager::find() will always give you the current version of the document. The DocumentManager provides access to old versions with getAllLinearVersions($document) and findVersionByName($class, $id, $name). The later gives you a detached document with the values it had when the specified version was created. Using checkin/checkout/checkpoint you generate versions. Finally there is restoreVersion and removeVersion to restore the document to an old version resp. to remove something from the version history.

An example should help understanding the workflow:

$article = new Article();
$article->id = '/test';
$article->topic = 'Test';

// generate a version snapshot of the document as currently stored

$article->topic = 'Newvalue';

// get the version information
$versioninfos = $dm->getAllLinearVersions($article);
$firstVersion = reset($versioninfos);
// and use it to find the snapshot of an old version
$oldVersion = $dm->findVersionByName(null, $article->id, $firstVersion['name']);

echo $oldVersion->topic; // "Test"

// find the head version
$article = $dm->find('/test');
echo $article->topic; // "Newvalue"

// restore the head to the old version

// the article document is refreshed
echo $article->topic; // "Test"

// create a second version to demo removing a version
$article->topic = 'Newvalue';

// remove the old version from the history (not allowed for the last version)

symfony phpcr doctrine cmf