267

Is there a good object-relational-mapping library for PHP?

I know of PDO/ADO, but they seem to only provide abstraction of differences between database vendors not an actual mapping between the domain model and the relational model. I'm looking for a PHP library that functions similarly to the way Hibernate does for Java and NHibernate does for .NET.

Forer
  • 1,035
  • 1
  • 7
  • 32
sgibbons
  • 3,460
  • 11
  • 32
  • 31

36 Answers36

118

Look into Doctrine.

Doctrine 1.2 implements Active Record. Doctrine 2+ is a DataMapper ORM.

Also, check out Xyster. It's based on the Data Mapper pattern.

Also, take a look at DataMapper vs. Active Record.

Community
  • 1
  • 1
Ian P
  • 12,298
  • 6
  • 42
  • 68
103

Try RedBean, its requires:

  • No configuration
  • No database (it creates everything on the fly)
  • No models
  • etc.

It even does all the locking and transactions for you and monitors performance in the background. (Heck! it even does garbage collection....) Best of all... you don't have to write a single... line of code... Jesus this, ORM layer, saved me ass!

fncomp
  • 5,630
  • 2
  • 30
  • 41
  • 9
    redbean is hands down the best database abstraction layer i've ever worked with. not "one of the best" - the best. – Nir Gavish Feb 15 '10 at 18:15
  • Very nice find. I'm very impressed with this ORM to say the least – Christopher Tarquini May 18 '10 at 08:15
  • 6
    but: http://stackoverflow.com/questions/3212917/redbean-o-rm-store-date-as-varchar255 – Sirber Aug 02 '10 at 15:19
  • I just don't know yet if this thing even works with existing databases that good. Especially if they're interconnected through relationships... – sinni800 Aug 01 '11 at 09:53
  • 1
    Compare RedBean with Doctrine: http://stackoverflow.com/questions/8063640/redbean-vs-doctrine/ – PiTheNumber Nov 17 '11 at 09:44
  • 3
    +1 +1 +1 +! +! !!!!...jesus I read the first part of the documentation and it got me making sinister dictator laughter, and I'm downloading it already! – KJW Mar 27 '12 at 03:09
  • I'm not a PHP guy, but I have to admit that looks pretty slick. – NotMe Apr 26 '12 at 02:48
  • I really don't want to be a downer, but this goes against everything I know -- that database tables should be designed with care (for performance) and that `ALTER TABLE` calls are expensive (and potentially dangerous). – Nicole Jun 21 '12 at 03:18
  • @NickC Once you are finished developing the database can be 'frozen' to stop any more changes. – Sam Aug 11 '12 at 07:40
45

There are only two good ones: Doctrine and Propel. We favor Doctrine, and it works well with Symfony. However if you're looking for database support besides the main ones you'll have to write your own code.

hriziya
  • 970
  • 1
  • 18
  • 37
Ilya Kochetov
  • 16,969
  • 6
  • 41
  • 58
  • Propel is pretty good by php standards. It produces fairly clean code that is IDE friendly with getters and setters and a very clean Criteria abstraction system for queries. – 0x6A75616E Sep 02 '11 at 02:49
  • 2
    Since this question gets linked around SO quite a bit, I wanted to point out that Propel is a discontinued project as of 2020. PHP7 compatibility was never implemented. So choosing Propel as the ORM for a new software project in 2020 is not a good idea. – mrodo Feb 28 '20 at 00:27
  • I have been using Doctrine for a few years and hate it. Not because it doesn't work but it is hugely bloated, a memory hog, slow, and it generates a bazillion queries against your DB while performing operations in the background. It works, don't get me wrong, but you will be spending a lot of time learning it and its nuances that frankly are very frustrating if you have large dependent datasets. Doctrine is in wide use, no doubt, but if I were selecting an ORM today, Doctrine would NOT be my first choice, or even second ... JM5C. – WebTigers Sep 02 '20 at 15:07
  • 1
    @mrodo Propel is not discontinued. Propel version 2 is still in active development for PHP 7.2 and above. – bigtunacan Dec 02 '20 at 06:07
34

Axon ORM is part of the Fat-Free Framework - it features an on-the-fly mapper. No code generators. No stupid XML/YAML configuration files. It reads the database schema directly from the backend, so in most CRUD operations you don't even have to extend a base model. It works with all major PDO-supported database engines: MySQL, SQLite, SQL Server/Sybase, Oracle, PostgreSQL, etc.

/* SQL */
CREATE TABLE products (
    product_id INTEGER,
    description VARCHAR(128),
    PRIMARY KEY (product_id)
);

/* PHP */
// Create
$product=new Axon('products'); // Automatically reads the above schema
$product->product_id=123;
$product->description='Sofa bed';
$product->save(); // ORM knows it's a new record

// Retrieve
$product->load('product_id=123');
echo $product->description;

// Update
$product->description='A better sofa bed';
$product->save(); // ORM knows it's an existing record

// Delete
$product->erase();

Most of all, the plug-in and accompanying SQL data access layer are just as lightweight as the framework: 14 KB (Axon) + 6 KB (SQLdb). Fat-Free is just 55 KB.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
bcosca
  • 16,475
  • 5
  • 36
  • 50
28

I've been developing Pork.dbObject on my own. (A simple PHP ORM and Active Record implementation) The main reason is that I find most ORMs too heavy.

The main thought of Pork.dbObejct is to be light-weight and simple to set up. No bunch of XML files, just one function call in the constructor to bind it, and an addRelation or addCustomRelation to define a relation to another dbObject.

Give it a look: Pork.dbObject

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
SchizoDuckie
  • 9,224
  • 6
  • 31
  • 40
  • 1
    I was looking for a lightweight PHP ORM implementation today, and found Pork.dbObject thanks to this post. It works great! +1 – E Dominique Apr 14 '09 at 17:21
  • 6
    Duude! This is pretty interesting. I see that the latest update happened somewhere in '09. Is this still maintained? If not... I just might revive it :) – VladFr Dec 06 '10 at 14:22
22

Try Doctrine2. It's probably the most powerful ORM tool for PHP. I'm mentioning it separately from Doctrine 1, because it's a completely different piece of software. It's been rewritten from scratch, is still in beta phase, but it's usable now and developed.

It's a very complex ORM, but well designed. Lot of magic from original Doctrine 1 disappeared. It provides a complete solution, and you can write your own ORM on top of Doctrine2 or use just one of its layers.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Tom Pažourek
  • 7,390
  • 7
  • 51
  • 92
13

I just started with Kohana, and it seems the closest to Ruby on Rails without invoking all the complexity of multiple configuration files like with Propel.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Zak
  • 23,916
  • 10
  • 36
  • 65
  • I also agree that Kohana is the framework that is most similar to RoR in the PHP world. All it's missing is scaffolding, and with CLI support in KO3 it's just a matter of someone rolling up their sleeves and doing it. – Phillip Whelan May 27 '11 at 03:53
12

Check out Outlet ORM. It is simpler than Propel and Doctrine and it works similar to Hibernate, only with more of a PHP feel to it.

Alvaro
  • 475
  • 3
  • 7
  • 3
    I tried this. I had to specify the same object properties in 3 places - config, model and database schema. That's a lot of work for implementing an ORM IMO. – mixdev Jun 15 '10 at 23:32
  • Outlet is very configuration-heavy. – Lotus Notes Jul 15 '11 at 23:12
  • I've tried this (1.0 RC1) and it was very buggy even in the core functionalitty. And yes, there is a lot of configuration to write. I don't recommend it. – Szymon Wygnański Aug 09 '11 at 19:54
11

I really like Propel, here you can get an overview, the documentation is pretty good, and you can get it through PEAR or SVN.

You only need a working PHP5 install, and Phing to start generating classes.

Christian C. Salvadó
  • 723,813
  • 173
  • 899
  • 828
  • Propel can also 'reverse engineer' an existing database schema - creating PHP objects from reading the database schema. – David Goodwin Jul 05 '11 at 10:17
8

I found ORM related classes in the PHP library Flourish.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
VDVLeon
  • 1,405
  • 1
  • 14
  • 26
7

You should check out Idiorm and Paris.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
th3mus1cman
  • 406
  • 4
  • 6
6

Give a shot to dORM, an object relational mapper for PHP 5. It supports all kinds of relationships (1-to-1), (1-to-many), (many-to-many) and data types. It is completely unobtrusive: no code generation or class extending required. In my opinion it is superior to any ORM out there, Doctrine and Propel included. However, it is still in beta and might change significantly in the next couple months. http://www.getdorm.com

It also has a very small learning curve. The three main methods you will use are:

<?php 
$object = $dorm->getClassName('id_here');
$dorm->save($object);
$dorm->delete($object);
Olivier Lalonde
  • 17,330
  • 28
  • 69
  • 86
4

Tried the ORM of Flourish library.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
eaguilar
  • 87
  • 1
  • 2
  • Yes, I've been working with it for some time. The flourishlib is great, but the ORM still has some work to do. Working with join tables that has additional properties or foreign keys, creating new objects can be a bit tedious. Working with simple models is a breeze. Have a look at Repose ORM or Outlet for an alternative ORM for PHP. – Michael Oct 12 '10 at 11:10
4

I am currently working on phpDataMapper, which is an ORM designed to have simple syntax like Ruby's Datamapper project. It's still in early development as well, but it works great.

Vance Lucas
  • 2,688
  • 1
  • 23
  • 21
4

I have had great experiences with Idiorm and Paris. Idiorm is a small, simple ORM library. Paris is an equally simple Active Record implementation built on Idiorm. It's for PHP 5.2+ with PDO. It's perfect if you want something simple that you can just drop into an existing application.

Sander Marechal
  • 22,170
  • 10
  • 59
  • 90
3

Try PHP ADOdb.

I can't say it's the best, because I haven't used the others. But it's fast, it supports Memcached and caching.

And it's waaaay faster than Zend Framework's DB/Select.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
  • 2
    adodb lends itself more to the Thin Model/Fat Controller flavor, which is generally not a good thing. – jblue Sep 20 '10 at 10:14
  • ADOdb has an ORM (but isn't just an ORM). It's a really great solution generally, it works much better than Zend does for DB (as well as being slower than ADOdb, Zend DB has only limited JOIN support), it supports auto escaping with parameterisation (unlike say Doctrine) many different DB backends and has nice extendable caching design with super easy memcache integration. I don't think it's at all accurate to say it lends it self to a "Thin Model/Fat Controller" implementation (you can do that or not, but ADOdb's design doesn't favour one way or the other). – Iain Collins May 06 '12 at 16:40
3

Until PHP 5.3 release don't expect to have a good ORM. It's a OO limitation of PHP.

knoopx
  • 15,557
  • 7
  • 34
  • 41
  • So how would PHP 5.3 help someone write a better ORM? I don't see any reason. – Ionuț G. Stan Mar 01 '09 at 12:11
  • 8
    the main reason is the introduction of late static binding ("static" keyword). read about it on http://blog.felho.hu/what-is-new-in-php-53-part-2-late-static-binding.html – knoopx Mar 07 '09 at 11:51
  • 2
    ORM don't really need static variables, they can be well designed using instance variables only. – Tom Pažourek Jul 23 '10 at 21:55
  • True, late static binding is the reason I can get each ORM instance of my [own ORM so low](https://github.com/xeoncross/micromvc). Before late static binding it was as wasteful as most others. – Xeoncross Feb 23 '12 at 22:29
3

My friend Kien and I have improved upon an earlier version of an ORM that he had written prior to PHP 5.3. We have essentially ported over Ruby on Rails' Active Record to PHP. It is still lacking some key features we want such as transactions, composite primary key support, a few more adapters (only MySQL and SQLite 3 work right now). But, we are very close to finishing this stuff up. You can take a look at PHP ActiveRecord with PHP 5.3.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
3

Have a look at the LEAP ORM for Kohana. It works with a bunch of databases, including DB2, Drizzle, Firebird, MariaDB, SQL Server, MySQL, Oracle, PostgreSQL, and SQLite. With a simple autoload function, it can work with almost any PHP framework. The source code is on GitHub at https://github.com/spadefoot/kohana-orm-leap. You can checkout LEAP's tutorials online.

The ORM library works with non-integer primary keys and composite keys. Connections are managed via a database connection pool and it works with raw SQL queries. The ORM even has a query builder that makes building SQL statements super simple.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Matthew
  • 223
  • 2
  • 5
2

NotORM

include "NotORM.php";
 $pdo = new PDO("mysql:dbname=software");
 $db = new NotORM($pdo);
 $applications = $db->application()
->select("id, title")
->where("web LIKE ?", "http://%")
->order("title")
->limit(10)
;
foreach ($applications as $id => $application) {
echo "$application[title]\n";
}
worenga
  • 5,616
  • 2
  • 24
  • 45
2

PHP ORM Faces For PDO extension. See PHP Faces Framework.

$urun = new Product();
$urun->name='CPU'
$urun->prince='124';
$urun->save();
d33tah
  • 8,728
  • 10
  • 51
  • 128
Kurt
  • 1
  • 2
2

You can check out Repose if you are feeling adventurous. Like Outlet, it is modeled after Hibernate.

It is still very early in its development, but so far the only restrictions on the domain model are that the classes are not marked final and properties are not marked private. Once I get into the land of PHP >= 5.3, I'll try to implement support for private properties as well.

Beau Simensen
  • 4,458
  • 2
  • 34
  • 55
2

If you are looking for an ORM that implements the Data Mapper paradigm rather than Active Record specifically, then I would strongly suggest that you take a look at GacelaPHP.

Gacela features:

  • Data mapper
  • Foreign key mapping
  • Association mapping
  • Dependent mapping
  • Concrete table inheritance
  • Query object
  • Metadata mapping
  • Lazy & eager loading
  • Full Memcached support

Other ORM solutions are too bloated or have burdensome limitations when developing anything remotely complicated. Gacela resolves the limitations of the active record approach by implementing the Data Mapper Pattern while keeping bloat to a minimum by using PDO for all interactions with the database and Memcached.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Noah Goodrich
  • 23,504
  • 12
  • 61
  • 95
2

Brazilian ORM: http://www.hufersil.com.br/lumine. It works with PHP 5.2+. In my opinion, it is the best choice for Portuguese and Brazilian people, because it has easy-to-understand documentation and a lot of examples for download.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
2

Agile Toolkit has its own unique implementation of ORM/ActiveRecord and dynamic SQL.

Introduction: http://agiletoolkit.org/intro/1

Syntax (Active Record):

$emp=$this->add('Model_Employee');
$emp['name']='John';
$emp['salary']=500;
$emp->save();

Syntax (Dynamic SQL):

$result = $emp->count()->where('salary','>',400)->getOne();

While Dynamic SQL and Active Record/ORM is usable directly, Agile Toolkit further integrates them with User Interface and jQuery UI. This is similar to JSF but written in pure PHP.

$this->add('CRUD')->setModel('Employee');

This will display AJAXified CRUD with for Employee model.

romaninsh
  • 10,068
  • 4
  • 46
  • 70
2

MicroMVC has a 13 KB ORM that only relies on a 8 KB database class. It also returns all results as ORM objects themselves and uses late static binding to avoid embedding information about the current object's table and meta data into each object. This results in the cheapest ORM overhead there is.

It works with MySQL, PostgreSQL, and SQLite.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Xeoncross
  • 50,836
  • 73
  • 238
  • 351
1

Doctrine is probably your best bet. Prior to Doctrine, DB_DataObject was essentially the only other utility that was open sourced.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
1

I work on miniOrm. Just a mini ORM, for using Object Model & MySQL Abstraction Layer as simply as possible. Hope it may help you : http://jelnivo.fr/miniOrm/

1

There's a fantastic ORM included in the QCubed framework; it's based on code generation and scaffolding. Unlike the ActiveRecord that's based on reflection and is generally slow, code generation makes skeleton classes for you based on the database and lets you customize them afterward. It works like a charm.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Alex Weinstein
  • 9,499
  • 8
  • 38
  • 58
1

Look at http://code.google.com/p/lworm/ . It is a really simple, but powerful, lightweight ORM system for PHP. You can also easily extend it, if you want.

Faz
  • 186
  • 2
  • 5
1

A really good simple ORM is MyActiveRecord. MyActiveRecord documentation. I have been using it a lot and can say it's very simple and well tested.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Szymon Wygnański
  • 9,426
  • 6
  • 29
  • 44
1

Looked at Syrius ORM. It's a new ORM, the project was in a development stage, but in the next mouth it will be released in a 1.0 version.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
1

Try PdoMap. Wikipedia claims that is inspired by Hibernate. Since I never used Hibernate, I cannot judge :), but I would say from my experience that is good and fast ORM that is easy to implement, with a less steep learning curve that other ORMs.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Hydrino
  • 537
  • 3
  • 9
1

Another great open source PHP ORM that we use is PHPSmartDb. It is stable and makes your code more secure and clean. The database functionality within it is hands down the easiest I have ever used with PHP 5.3.

Joe
  • 59
  • 1
  • 2
1

Sado is a simple PHP ORM package, easy to use, and offers video tutorials

Shay Anderson
  • 822
  • 6
  • 5
1

If you are looking for an ORM, like Hibernate, you should have look at PMO.

It can be easily integrated in an SOA architecture (there is only a webservice classe to develop).

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123