PHP ORM

March 14, 2008

development php

Looks like this could be a winning ORM for PHP doctrine-project.org. You can using YAML to define the schema files and create the database from the classes. The thing that really got me excited though is at the end of the documentation, under Plugins, the AuditLog and versioning plugin that will automatically trac versions of any table. Here is the code for a simple new item object:

class NewsItem extends Doctrine_Record
{
  public function setTableDefinition()
  {
    $this->hasColumn('title', 'string', 200);
    $this->hasColumn('content', 'string');
  }
}

This is what it looks like if I want to maintain versions of every update

class NewsItem extends Doctrine_Record
{
  public function setTableDefinition()
  {
    $this->hasColumn('title', 'string', 200);
    $this->hasColumn('content', 'string');
    // the versioning plugin needs version column
    $this->hasColumn('version', 'integer');
  }

  public function setUp()
  {
    $this->actAs('Versionable');
  }
}

Simply by adding a column called ‘version’ and specifying $this->actAs('Versionable') I get version tracking for free. This then allows me to do things like:

$newsItem->revert(1);

I love it! There is also a soft-delete plugin, these two help massively towards implementing the idea of Never Use a Warning When you Mean Undo! I will be investing some time into getting to know this framework, thanks guys!