My friend Kien and I have improved upon an earlier version of an ORM that he had written prior to PHP 5.3. The ActiveRecord we have created is inspired by Ruby on Rails and we have tried to maintain their conventions while deviating mainly because of convenience or necessity.
You can read the full article about our implementation here:
http://www.derivante.com/2009/05/14/php-activerecord-with-php-53/
Basically there are only two configuration pieces you need:
declare the directory of the models
declare dsn strings like so -> array(‘development’ => ‘mysql://test:test@127.0.0.1/test’);
There is a config class that allows you to configure those settings so you can see that setup is easy. What this means if you have used doctrine or propel, you do not have to setup yaml/xml files describing your schema. All of that is done in the background for you.
Our implementation has features like validations, complex relationships, serializations (to_json, to_xml). Creating json out of a record from your database is easy:
class Book extends ActiveRecord\Model{
public function upper_title(){
return strtoupper($this->title);
}
}
#produces: {title: 'sharks wit lazers', author_id: 2}
$book = Book::find(1);
$book->to_json();
#produces: {title: 'sharks wit lazers'}
$book->to_json(array('except' => 'author_id'));
#produces: {upper_title: 'SHARKS WIT LAZERS'}
#make methods an array of methods and it will call them all
$book->to_json(array('methods' => 'upper_title', 'only' => 'upper_title'));
Validations are used to make sure that the data in your model is accurate so that you are not saving bad data to your db. Basically, you can declare that certain fields should have values that are x characters long or that a numeric field should be an integer between 10 and 20. Or, you can supply a regular expression to make sure that an e-mail field is a valid e-mail .
class Book extends ActiveRecord\Model
{
static $validates_format_of = array(
array('title', 'with' => '/^[a-zW]*$/', 'allow_blank' => true)
);
static $validates_exclusion_of = array(
array('title', 'in' => array('blah', 'alpha', 'bravo'))
);
static $validates_inclusion_of = array(
array('title', 'within' => array('tragedy of dubya', 'sharks wit laserz'))
);
static $validates_length_of = array(
array('title', 'within' => array(1, 5)),
array('attribute2', 'in' => array(1,2)),
array('attribute3', 'is' => 4, 'allow_null' => true)
);
# same as above since it is just an alias
static $validates_size_of = array();
static $validates_numericality_of = array(
array('title')
);
static $validates_presence_of = array(
array('title')
);
};
$book = new Book;
$book->title = 'this is not part of the inclusion';
if (!$book->save())
print_r($book->errors->on('title'));