I'm coming from .NET world back to PHP for some side projects. I am comfortable with PHP as a language, but am kind of lost in many PHP frameworks available today. Back in the days I did PHP we just wrote SQL queries, so I have no idea what is possible with PHP today in terms of ORM, therefore the question.
I got used to creating my database models using ADO.NET Entity Framework Code First and I like this approach, so I am looking for a PHP ORM framework with similar set of features.
If I understand you correctly, you are searching a framework with something like AR and CRUD.
I think almost every more famous PHP framework have this options.
Anyway, I am using Yii framework and I can say it's one of the best options, but you can surf a little to see which framework can fit your requirements.
You can create DB with table relations etc, and the generator will create your models + relations for the Active Record. Also CRUD generator can create and the View/Controller part.
You should review current PHP ORM frameworks, you may find something that they offer that the ADO.Net framework does not.
What is the easiest to use ORM framework for PHP?
This stack question is very detailed around your question: Good PHP ORM Library?.
Everyone has an opinion, some better than others.
have a look at Doctrine or Propel ORM
Well, seeing as I'm dissatisfied with Zend_Db_Table after being spoiled by LINQ, I'm looking to get started learning an ORM with PHP. General consensus seems to be that Doctrine and Propel are the only good ones for serious use -- and whatever my opinion, I'd like to use something at least moderately popular so that people in the future can look at this app I'm working on without having an head explosion :P
I'm currently leaning towards Propel because it's documentation seems to be a bit more complete, and it supports the nested set model (also called "modified preorder tree transversal model") right out of the box. However, I like Doctrine's use of namespaces and other PHP 5.3 features, and it seems to be a bit more popular.
From those who have used either ORM with Zend Framework, which meshes better with the existing framework (if either)? What kind of issues should I watch out for using either framework with Zend?
I can't speak for Propel but there are a lot of good integration resources for ZF and Doctrine. See Zend Framework 1.11 with Doctrine 2 Integration
Well Doctrine 1.2 supports Nested Set... much better than propel did in < 1.3. I havent used propel 1.4/5 so i cant comment on its current state. I also havent used Doctrine 2... ive been sticking with 1.2 since mot of my projects dont use php 5.3...
Overall I liked Propel better... I prefer generated classes to ones configured dynamically at runtime. Also keep i mind Propel is an active record implementation while Doctrine 2 uses the Data Mapper pattern.
I would like to know which one of the following is the best choice for O/R mapping in PHP:
Axon
CakePHP
Doctrine
Kohana PHP
lworm
PdoMap
Propel
Rocks
Qcodo
Redbean
Sphorm
Torpor
We are going to use PHP only for Web Service implementation. We have a Java background so a framework which is inspired by Hibernate would be easier for us to use as long as it's a good, well documented and more or less easy to use O/R mapping library.
Thank you!
The problem with this question is that it is hard to tell what is the best choice for your specific task and environment. Furthermore, a full comparison would require in-depth knowledge of all the alternatives.
As for Doctrine you'll be able to find a good piece of documentation. And it is fairly easy to get going.
Like Jensgram already notes, it is hard to tell which option suites your needs.
That said, I have experience with Kohanaphp and it's integrated ORM. I must say it works perfect, but it has limited functionality if you compare it to Doctrine2. If you need advanced options like inheritance mapping and proxy classes Doctrine is the way to go. Like Doctrine 2 introduction says:
Object relational mapper (ORM) for PHP
that sits on top of a powerful
database abstraction layer (DBAL). One
of its key features is the option to
write database queries in a
proprietary object oriented SQL
dialect called Doctrine Query Language
(DQL), inspired by Hibernates HQL.
This provides developers with a
powerful alternative to SQL that
maintains flexibility without
requiring unnecessary code
duplication.
As it says, it is inspired on Hibernate HQL. I don't have experience with the other options you mention, so I can't say anything usefull about those.
I'm building an application in the Zend Framework, but I'd like to implement a "Data Mapper" style ORM layer, constructing Model classes that only include the specific pieces of data they need to represent the domain concept (regardless of which tables those fields happen to belong to). Since my DB structure is highly normalized, the benefits gained from an Active Record ORM would be superficial.
So how would you implement a very simple, straightforward data mapper ORM layer in the Zend Framework?
I believe there's no true "Data Mapper" style ORM for PHP yet. If you want a true "Data Mapper", I think you might be out of luck.
There are 2 popular ORM in PHP, Propel and Doctrine. They are both more on the ActiveRecord side.
Doctrine in my own opinion is the go. At the moment, it's stable release 1.2 is not integrated with ZF yet. e.g. you cannot use the doctrine command line script to generate model classes for a modular ZF application setup.
However if you are running a single module ZF application, doctrine can be integrated pretty well. As #ArneRie pointed out, ZendCasts has got some really good videos. I learned a lot from it.
If you are interested, I also derived from it and made one of my own approach. You can find my blog post about this topic at http://blog.elinkmedia.net.au/2009/12/03/zf-doctrine-and-unit-tests/. You can download the source code of my sample app from github too.
Doctrine 2, which is currently in alpha (beta due 1 March 2010), is a data mapper orm inspired by JPA/Hibernate. Zend Framework has shelved their own Zend Entity component in favour of integrating Doctrine 2 with Zend Framework.
Depending on your timescales, you might want to look at Doctrine 2.
i would suggest to use Doctrine with the Zend Framework if you want to use an ORM. There are good Tutorials and Screencasts around with a lot of information.
Doctrine 1.2 with Zend Framework Screencast
The implementation details for creating an ORM can be quite extensive, and I lack the experience to give any meaningful recommendations beyond suggesting already existing projects, so I will suggest one instead.
The Data Mapper pattern is not very popular in the PHP world (in favour of Active Record, and certainly much of that favour is attributed to the fawning of Ruby on Rails in the PHP framework community), although there exists one notable project -- and it looks promising.
Outlet ORM follows the Data Mapper pattern, although I am not sure if it would be sufficient for your needs. I have so far had no problems with it.
This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.
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.
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.
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!
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.
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.
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
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.
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.
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.
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.
I found ORM related classes in the PHP library Flourish.
You should check out Idiorm and Paris.
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);
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.
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.
Tried the ORM of Flourish library.
Until PHP 5.3 release don't expect to have a good ORM. It's a OO limitation of PHP.
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.
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.
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.
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.
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.
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.
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.
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.
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";
}
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.
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.
Doctrine is probably your best bet. Prior to Doctrine, DB_DataObject was essentially the only other utility that was open sourced.
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).
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.