Recently I’ve been learning Ruby, and at the same time Rails, with it I’ve learnt alot of good standardised ways of things to do, but without a doubt the most powerful part of Rails, for me, was ActiveRecord. So I had a search around the net and came across this fantastic little ActiveRecord implementation in PHP, called php.activerecord. In fact its so amazing I thought I would write a little guide for you to get up and running quickly with it.

We are going to create a quick and dirty model that will represent a blog post.

Please note: the version of Zend Framework when this article was written was 1.10.6

Step 1 - Download

Download php.activerecord from (version as of writing this is 1.0)

Step 2 - Extract

Extract the file, and place it somewhere on your webserver (I put mine in my “library” directory, along with Zend Framework)

Step 3 - Bootstrap

Open your Bootstrap.php file for your application, add a protected function for initialising php.activerecord:

protected function _initActiveRecord() {
	ActiveRecord\\Config::initialize(function($cfg) {
			'development' => 'mysql://username:password@host/development_db_name',
			'production' => 'mysql://username:password@host/production_db_name',

Step 4 - Database table

Set up a database table called ‘posts’ (notice the plural, this is because it holds multiple “Post” models).

	`created_at` datetime DEFAULT NULL,
	`updated_at` datetime DEFAULT NULL,
	`title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
	`excerpt` text COLLATE utf8_unicode_ci,
	`content` text COLLATE utf8_unicode_ci,

Step 5 - Model

Now create your model, under the “models” directory which we created during the initialisation of php.activerecord that will look something like this (notice Post is singular, this is because your model represents a singular version of what it represents)


	class Post extends ActiveRecord\\Model {



Step 6 - Away you go!

Now you have a guide how to get up and running, take a look at the various guides available on the php.activerecord wiki to see how you would use it within your controllers.

If you find this useful, then please leave a comment and let me know. If lots of people enjoy it then I’ll create a step by step tutorial on how to get something up and running using it. Also if you have any questions then leave a comment and I’ll do my best to help wherever I can.

Published on Monday, 12 July 2010