When developing a new software we should strive to make a balance between future complexity and current usability, this primarily translates into any database related development. You should always consider a possibility that a database, you use today, may be inappropriate somewhere in the future. Do you really want to rewrite your code just to be able to use another database? If your answer is no, then you are at the right place.
 
This problem creates a need for a database class library to hide the differences between the different database API’s (encapsulate the differences) so we can easily switch databases. Several different approaches exist, and we will discuss one of them: ORM or Object Relational Model. This technique is used to convert the data from an incompatible type to that of a relational database. The mapping is done by using the OOP languages (PHP for example).
 
This article will cover best currently known PHP ORM libraries, but I will not cover forgotten or abandoned libraries. Only stand-alone or modular ORM libraries will be included (except library once known as Axon ORM, which is part of a Fat Free framework and is not available on its own). Few of them are incorporated in other larger frameworks, but at the same time, are also available on they own.
 
I will try to be as objective as I can. Before I chose these frameworks, I spent a lot of time searching for the perfect one. Everything said here doesn’t mean other frameworks are not worthy or inadequate. Please notify me if you find any technical or information inaccuracies (or you think I missed some libraries). I will update this article to address them, leave me a comment down below or you can even mail me to dragan.gaic@gmail.com.
 
This article will cover first five ORM libraries, next five can be found in the follow-up article.
 
 

Note: If this tutorial was helpful, need further clarification, something is not working or do you have a request for another Ionic post? Furthermore, if you don't like something about this blog, if something is bugging you, don't like how I'm doing stuff here, again leave me a comment below. I'm here to help you, I expect the same from you. Feel free to comment below, subscribe to my blog, mail me to dragan.gaic@gmail.com, or follow and mention me on twitter (@gajotres). Thanks and have a nice day!

PS. If you want my help, if possible (even if it takes you some time to do that), create a working example I can play with. Use Plunker for AngularJS based questions or jsFiddle for jQuery/jQuery Mobile based questions.


 
Let’s look at a list of my chosen 10, in alphabetical order:
 
 
 
1. ADOdb Active Record
 
ADOdb is a PHP database class library to provide more powerful abstractions for performing queries and managing databases. ADOdb also hides the differences between the different databases so you can easily switch databases without changing the code. It is also a data abstraction layer, but it provides an ORM functionality in the form of ADOdb Active Record.
 

Links

 
 
Official Site GitHub Documentation
 
 

Pro

 
  • Released under both BSD and GNU Lesser GPL library license. You can use it in proprietary products.
  • Fast, stable and widely tested
  • Supports a large number of databases (MySQL, PostgreSQL, Interbase, Firebird, Informix, Oracle, MS SQL, Foxpro, Access, ADO, Sybase, FrontBase, DB2, SAP DB, SQLite, Netezza, LDAP and many more), here’s a full list.
  • Supports PHP 4.0 or later, so it’s excellent if you’re version restricted (ADOdb supports only PHP 5.0 or later)
  • Supports one-to-many relationships
  • Excellent library for smaller/medium projects
 

Con

 
  • Good framework requires a decent documentation; this one is confusing as hell. Only one page is used to cover everything ORM related.
  • It is never going to be as fast as the native database functions

 

Example

 
include_once('../adodb.inc.php');
include_once('../adodb-active-record.inc.php');

$db = NewADOConnection('mysql://root@localhost/northwind');
ADOdb_Active_Record::SetDatabaseAdapter($db);

$db->Execute("CREATE TEMPORARY TABLE `persons` (
				`id` int(10) unsigned NOT NULL auto_increment,
				`name_first` varchar(100) NOT NULL default '',
				`name_last` varchar(100) NOT NULL default '',
				`favorite_color` varchar(100) NOT NULL default '',
				PRIMARY KEY  (`id`)
			) ENGINE=MyISAM;
		   ");
		   
$db->Execute("CREATE TEMPORARY TABLE `children` (
				`id` int(10) unsigned NOT NULL auto_increment,
				`person_id` int(10) unsigned NOT NULL,
				`gender` varchar(10) default 'F',
				`name_first` varchar(100) NOT NULL default '',
				`name_last` varchar(100) NOT NULL default '',
				`favorite_pet` varchar(100) NOT NULL default '',
				PRIMARY KEY  (`id`)
			) ENGINE=MyISAM;
		   ");
		   
$db->Execute("insert into children (person_id,name_first,name_last) values (1,'Jill','Lim')");
$db->Execute("insert into children (person_id,name_first,name_last) values (1,'Joan','Lim')");
$db->Execute("insert into children (person_id,name_first,name_last) values (1,'JAMIE','Lim')");
		   
class person extends ADOdb_Active_Record{}
ADODB_Active_Record::ClassHasMany('person', 'children','person_id');	

$person = new person();

$person->name_first     = 'John';
$person->name_last      = 'Lim';
$person->favorite_color = 'lavender';
$person->save(); // this save will perform an INSERT successfully

$person2 = new person(); # no need to define HasMany() again, adodb remembers definition
$person2->Load('id=1');

$c = $person2->children;
if (is_array($c) && sizeof($c) == 3 && $c[0]->name_first=='Jill' && $c[1]->name_first=='Joan'
	&& $c[2]->name_first == 'JAMIE') echo "OK Loaded HasMany<br>";
else {
	echo "Error loading hasMany should have three array elements Jill Joan Jamie<br>";
}
 

Notes

 
While ADOdb is an excellent data abstraction layer library I would not use it as an ORM, it just can’t compete with Doctrine2 or Propel2. It should be only used as a data abstraction layer library, nothing more, nothing less.
 
 
 
2. Axon ORM
Fat-Free Framework Logo
 
 
Official Site GitHub Documentation
 
 
This is an ORM included into the Fat-Free framework. Unfortunately, you can’t use it as a stand-alone library. I’ve included it here only because I’ve got a great respect for Fat-Free Framework (I like it, it’s light and efficient, everything you need for a smaller project).
 
Better name for this library would be ORM library once known as Axon. For some reason, it’s not called Axon anymore; it’s only referenced as an ORM, Smart ORM or mini-ORM.
 

Pro

 
  • Light and simple to use
  • Excellent library for smaller projects
  • Supports MySQL, SQLite, SQL Server, Sybase, and Oracle
  • Data Mapper structure derives directly from the database schema. No guesswork is involved.
 

Con

 
  • This library can barely pass as an ORM (barebone implementation), it’s called mini-ORM with reason.
  • It has no model classes so don’t expect database table generation based on model classes.
  • It does not provide methods for directly connecting objects to each other, i.e. SQL joins. You should use views instead.

 

Example

 
$db=new DB\SQL(
    'mysql:host=localhost;port=3306;dbname=mysqldb',
    'admin',
    'p455w0rD'
);

$item=new DB\SQL\Mapper($db,'products');
$item->totalprice='unitprice*quantity';
$item->load(array('productID=:pid',':pid'=>'apple'));
echo $item->totalprice;
 

Notes

 
If you are working with Fat-Free Framework, consider using any other available ORM.
 
Continue Reading

  • Konstantin Starojitski

    You write about Eloquent that 'It losses inherited advantages when used outside Laravel'.
    What exactly to you mean by this? Which advantages? I am asking because i am considering using Eloquent without Laravel.

  • “It is never going to be as fast as the native database functions”

    This is not a real con of one particular orm, its more like a general disadvantage of using orms …

  • Thanks I will try to review it.

  • Thank you I will check it and review it

  • Gerald Pasion

    me, as a first time ORM user. what do you recommend the most?

  • Cake3 has a nice ORM which is also standalone 🙂 . Another one if you love experiments are Atlas.Orm .

  • 666