PHP ORMs: Doctrine vs. Propel - php

I'm starting a new project with symfony which is readily integrated with Doctrine and Propel, but I of course need to make a choice.... I was wondering if more experienced people out there have general pros and/or cons for going with either of these two?
Thanks a lot.
EDIT:
Thanks for the all the responses, useful stuff. There's no truly correct answer to this question so I'll just mark as approved the one that got the most popular up-votes.

I'd go with Doctrine. It seems to me that it is a much more active project and being the default ORM for symfony it is better supported (even though officially the ORMs are considered equal).
Furthermore I better like the way you work with queries (DQL instead of Criteria):
<?php
// Propel
$c = new Criteria();
$c->add(ExamplePeer::ID, 20);
$items = ExamplePeer::doSelectJoinFoobar($c);
// Doctrine
$items = Doctrine_Query::create()
->from('Example e')
->leftJoin('e.Foobar')
->where('e.id = ?', 20)
->execute();
?>
(Doctrine's implementation is much more intuitive to me).
Also, I really prefer the way you manage relations in Doctrine.
I think this page from the Doctrine documentation is worth a read: http://www.doctrine-project.org/documentation/manual/1_2/en/introduction:doctrine-explained
To sum up: If I were starting a new project or had to choose between learning Doctrine and Propel I'd go for Doctrine any day.

I am biased, since I help a little bit on the next release of Propel, but you must consider that Propel was indeed the first ORM available, then lagged a bit when Doctrine got created, but now has active development again. Symfony 1.3/1.4 comes with Propel 1.4, where most comparisons stop at Propel 1.3. Also, the next release of Propel (1.5) will contain a lot of improvements, especially in the creation of you Criteria (resulting in less code for you to write).
I like Propel because it seems to be less complex than Doctrine: most code is in the few generated classes, whereas Doctrine has split up the functionality in lots of classes. I like to have a good understanding of the libraries I am using (not too much "magic"), but of course, I have more experience with Propel, so maybe Doctrine is not so complicated behind the scenes. Some say Propel is faster, but you should check this for yourself, and consider whether this outweighs other differences.
Maybe you should also consider the availability of Symfony plugins for the different frameworks. I believe Propel has an advantage here, but I don't know how many of the listed plugins are still up-to-date with the latest version of Symfony.

It comes down to personal preference.
I use Propel because (among other things) I like the fact that everything has its own concrete getter & setter method. In Doctrine, this is not the case.
Propel:
$person->setName('Derek');
echo $person->getName();
Doctrine:
$person->name = 'Derek';
echo $person->name;
The reason I like having getters & setters is that I can put all kinds of logic in them, if I need to. But that's just my personal preference.
I should also add that although Propel was slow-moving in the past, it is now under active development again. It has released several new versions in the past few months. The most recent version of Propel includes a "fluent query interface" similar to Doctrine's, so you don't have to use Criteria anymore if you don't want to.

It should be noted Doctrine 2 is currently in development released [ed] and functions almost completely different from the current stable version of Doctrine 1. It relies on the Data Mapper pattern instead of Active Record, and uses an 'entity manager' to handle persistence logic. When released it will bear closer resemblance to Java's Hibernate (Doctrine 1 is more like Rails' ActiveRecord).
I've been developing with the alpha release of Doctrine 2, and must say it is heads and shoulders above Doctrine 1 (just my opinion, and I've never used Propel). Chances are good that the Doctrine community will move toward it when it's released.
I would encourage you to check out Doctrine, but if you prefer the Active Record style that Propel and Doctrine use now, you might want to just stick with Propel.

The two references are somewhat outdated so you nevertheless cover some generalities, basically you'd have to evaluate your experience with the framework as such, a major drawback to doctrine is the inability to have an IDE that lets you auto-code in that propel is a winner, learning curves propel and doctrine are very different, it is easier to propel, if your project will need to manage complex data model uses doctrine, if you want to work quickly with an ORM which is best documented and find more support in Propel Internet uses, is much more mature and I believe that most used.
http://propel.posterous.com/propel-141-is-out

I'd suggest to use propel 1.6 which is better for IDE's autocomplete function.

I'm not a user of PHP 5 non-framework ORM, but here's some good comparison posts (in case you haven't seen them yet):
http://codeutopia.net/blog/2009/05/16/doctrine-vs-propel-2009-update/
http://trac.symfony-project.org/wiki/ComparingPropelAndDoctrine
Both conlusion favorite towards Doctrine as a newer generation of ORM for Symfony.

After using both of them for a number of years I prefer Propel 2 to Doctrine simply based on how you construct your query logic. Doctrine is as in depth as it can get and managing many aspects of it match that level of depth. Propel I feel has a more fluid and object driven way of building and managing the query interactions.
For me this led to less code in the model and more structures around how logic can/will be processed. This resulted in just building out many interactions as common functionality. (After all 90% of what you will do with a database is just going to be some degree of crud operation.)
In the end, both are powerful, manageable and will get the job done. My personal projects and interest use Propel ORM 2 and future projects, if still written in PHP will go that route.
I've been using both on a daily basis for the past 3-4 years.

I'd suggest using DbFinder Plugin. This is actually a very powerful plugin that supports both, and is quite a nice powerful. I actually like using it better than either.

If I'm not wrong, both ORMs use XML-based schema, and creating these schema definition is pretty cumbersome. If you need a PHP-based simple schema with fluent style. You may try LazyRecord https://github.com/c9s/LazyRecord it supports automatic migration and upgrade/downgrade script generators. And all the class files are generated statically without runtime cost.

Related

There is a php ORM which is similar to Rails ActiveRecord?

I'm designing an e-commerce database for a school project that require usage of cakePHP.
The ORM « attempt » of cake (yeah, it's not really an ORM) doesn't fit my needs.
I tried doctrine 2, I'm not convinced by the complex DSL and some crazy behaviors.
What I'm searching is an ORM that make associations effort-less and use conventions like we can do it with Rails ActiveRecord.
Because of complexity of an e-commerce database, I can't test everything and advices on doing that in PHP will be appreciated
Just wanted to share our experience with PHP ActiveRecord.
We are using it in production on a commercial web app for 2 years. We chose it for its unmatched simplicity and because it uses PHP5.3 features to provide nice RoR-like methods.
For 2 years we never had any significant issues with the library. We update it from time to time to keep up to date and pretty happy with it.
PHP ActiveRecord won't suite everybody since, well, it's ActiveRecord, not true ORM. But as you are specifically looking for RoR-like ActiveRecord solution in PHP, look no further.
Laravel's Eloquent ORM is nice, it can take a little bit of work to get it standalone but it's worth it.
You may wish to search for composer ActiveRecord packages, there are dozens of them:
One of them which says it was inspired by RoR: https://github.com/jpfuentes2/php-activerecord
There are dozens of them: https://packagist.org/search/?q=activerecord
Or even try ruby on rails in your search https://packagist.org/search/?q=activerecord%20ruby%20on%20rails
Try at least a bit of your own research before asking questions, as well as showing what you have found and tried.

PHP Framework and ORM vs Stored Procedures

I am in the process of picking a PHP framework for a web application I am starting. I have never really used a framework in the past but with this project there is a great need.
I have been debating between the usual suspects; CakePHP, Zend Framework and Symfony. I have been going back and forth about which framework will work best for me and this project. I am leaning towards CakePHP but I am still researching.
My question is not what framework is best. I know there is no real answer to that and there are tons of posts related to this subject. My question is related to the Model and ORM. I have read a lot about ORMs being slow and I am concerned about speed. I am very comfortable writing SQL and in the past have tried to keep all of my database interactions in stored procedures.
I am looking for some feedback about using CakePHP's ORM or Doctrine with Zend or Symfony as apposed to keeping everything in stored procedures. I know stored procedures are going to be faster but what else will I loose if I do not use an ORM? I understand that an ORM will give me database abstraction but in my mind that just helps people who do not write SQL. I also know that I do not know enough about ORMs.
If anyone can give me some feedback about this and which framework might be best based on using or not using an ORM.
Thanks for any help in advance.
0) Bang for effort
The key advantage with an ORM is that you don't spend as much time dealing with the persistence layer. A pre-written ORM ( I have worked with EclipseLink) will provide a ton of things you probably won't get in custom written stored procs. I think it's worth thinking about how much time you want to spend writing your persistence layer.
1) Caching
All the major ORMs provide multi-level distributed caches. Combined with Named/Predefined queries you can get SQL queries that don't actually have to go to the database. This can give you excellent performance.
2) Abstraction
ORMs allow you to define your table layout in one location and then they manage all the painful mapping between columns/tables and objects. Some will allow you to remap column, table and schema names without changing any code at all. If you work with people who like to change things around this can really simplify things.
3) Speed
Some ORMs can have bad performance, but it really is based on how you use it. I find that you tend to end up over-querying for things. On the other hand, you get things like built-in query profilers. You can write custom SQL for queries if you find you aren't getting the performance you need.
Mark Robinson gives a great response. I'm just going to back up what he says by giving our experience with Doctrine2.
I chose to use Doctrine2 as our ORM with Zend Framework a little while back. Our project is still being developed, but choosing D2 has been a decision we've not regretted one bit. Whilst you still need to give a lot of thought to your data architecture, D2 gives you the flexibility to be able to modify that model at a later date if needs be. It allowed us to try things out quickly in the early stages and the room to grow and change later when we decided that things weren't quite right - it happens.
In relation to Mark's point about abstraction. One of the other things I love about D2 is that we're working with plain old PHP objects. Don't underestimate the power of being able to think in terms of objects - both for the people responsible for modelling the data and the developers who work with the data - it'll make your life easier, trust me. Also, having inline documentation of the ORM mapping (if you choose the docblock approach) is nice.
Right, performance. As Mark says, there are ways and means to speed things up - but there's always going to be some overhead. Whenever you introduce another software layer, there'll be some performance hit, but it's a tradeoff. For us, the tradeoff - the advantages of using the ORM vs performance - is worth it. We'd spend more time debugging code and not getting things done without the ORM.
Anyway, D2 can help you with caching for queries, results and metadata. Whilst you probably just want an array cache during development, it's great that the facility for things like APC, memcache etc. is there when you go to test and deploy. You could even develop your own if you're brave.
http://www.doctrine-project.org/docs/orm/2.0/en/reference/caching.html
Hope that helps, I've probably missed stuff, but if you have any questions just fire them in and I'll do my best.
A framework implements mainly three kinds of features :
the flow between "getting a request" and "rendering a page". That's were you put things like MVC, router, etc...
the way to manage your model and it's persistence. That's where you see acronyms like ORM, DBAL, DAO
Components. Features, often working also standalone. Like Xml parsing, i18n handling, pdf generation...
When you choose your framework, it in facts means that you choose 1). It's the thing you will certainly have to stick with, it's the flow of your application. 2) and 3) ? You can integrate those you prefer. As an example, i'm on Zend Framework with most of it's components, but use Doctrine ORM 2 and Symfony's Dependency injection. A friend of mine is on Symfony 2, uses Doctrine ORM too, but does it's pdf generation and mail management with Zend's related components.
The other thing you need to know if that currently there is a "second generation" of php frameworks/orm's, (re)written to take advantage of the new php 5.3 features, and/or to solve the general performance/coupling issues they (nearly) all had. Some of them are production ready, some are still under development :
Doctrine ORM 2 (production ready)
Symfony 2 (production ready)
CakePhp 2 (in RC 2 currently, but by the time your project is ready it should be stable)
Zend Framework 2 (still under active development, but normally not for so long)
FLOW 3 (beta2, should be ready soon too)
For the ORM part, i'll recommend using one, especially Doctrine's. #Mark and #iainp999 explained perfectly why.
ORMs are for programmers who don't "grock" SQL!
Yes ORMs make it easier (or at least require less lines of code) to write simple CRUD stuff, but when you get to more complex requirements its like trying to write SQL with a piece of wet spaghetti from ten feet away.
So stick with SQL.
Its worth looking at something like "SQLMap" which is ORM starting from the "R"elational side of the mapping (most try to map an "O"bject on to a table). This will allow you to write the SQL yourself and generate the appropriate "helper" classes to easily access the results in your program.

What is a good database model definition language?

We are developing a substantial PHP web application using the Zend framework.
At this time, the product is starting to stabilize and we are moving away from quick-and-dirty setups in order to avoid regressions. In particular, we are now working on top of a single shared database that everyone edits. We want to get rid of this ASAP.
The Zend framework does not seem to support the classic RAD (Djangoish) mechanism where you define your data models and then it creates the tables for you. So we are thinking of using an external ORM tool that will do this.
We could have our schemas and initial fixtures defined in plain SQL but this is a) verbose b) error prone c) too low level and d) problematic because we must maintain different versions for every supported database backend.
So we are thinking of using an ORM like Doctrine or Propel to define our models and create tables with their initial data using the chosen framework's dialect. The application uses the Zend tool for ORMing so consistency between both tools would have to be maintained manually, but since changes are more gradual now this doesn't seem like much of a problem.
So, far, we are evaluating Doctrine and Propel for this task. Any suggestions about other ORMs that we missed? Maybe a different approach altogether for the task at hand?
Thanks!
Gonzalo
I really love RedBean. You keep your database models in pure php and it keeps track of everything.
RedBeanPHP is an open source ORM tool
for PHP. It focuses on simplicity and
ease of use. What makes RedBean unique
is that it creates your database
schema on-the-fly. It scans your data
and adjusts the column types to fit
your object properties. If your models
are stabilized you can freeze the
database. This way RedBean is easy to
develop with but is also extremely
fast on production servers.
Since I've found this ORM, I don't use doctrine any more.
http://redbeanphp.com/

Which PHP ORM works best with Zend Framework?

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.

Good PHP ORM Library?

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.

Categories