Why I switched to Doctrine

As I said in my previous post, I would tell more about why I switched to Doctrine. I mainly started to think about the idea when I first heard that we were going to start a new project in Symfony at work. The lead developer decided to go for Doctrine ORM instead of Propel. So I wanted to know why go went for Doctrine. I already knew that Doctrine is way faster then Propel 1.2 and it has more possibilities, but I just wanted to hear it from him. And I have to admit, he had good arguments.

The verbosity of the Doctrine code is way better then Propel. The model mappings (in line of XML files or YAML) are bigger for Propel then for Doctrine, and in Doctrine you can do everything in PHP instead of using external files (consolidated code is a huge plus).

The API of Propel is also “time-consuming”. Check the following Propel code:

$c = new Criteria(); 
$cton1 = $c->getNewCriterion(AuthorPeer::FIRST_NAME, "Leo");
$cton2 = $c->getNewCriterion(AuthorPeer::LAST_NAME, array("T1", "T2", "T3"), Criteria::IN);
$cton1->addOr($cton2); // add to Criteria
$c->add($cton1); 
$authors = AuthorPeer::doSelect($c);

with this Doctrine code:

$a = $conn->query("FROM Author a WHERE a.first_name = ? OR a.last_name IN ? ", array('Leo',array("T1", "T2", "T3"));

Propel doesn’t support his own query definition language like Doctrine, so every query has to be done through time-consuming API calls (and Doctrine also supports a object oriented API, so it is available if you need it). But it gets a lot more uglier if you when you need associations and eager loading in your queries, what can be run smoothly in Doctrine.

Doctrine supports 3 types of inheritance that you find back in Hibernate, where Propel is only limited in single-table-inheritance. This can be very useful in the implementation of complex systems. Doctrine support a sort of mixins (this at the act_as_* plugins of ActiveRecord), in a language that doesn’t support mixins :). Even think about the “event listeners” like the before_save etc methods in ActiveRecord. (long live Rails right )

Doctrine even supports migrations, and in the near future, they even will have automatic migrations (change the model definition, run auto-migrate and the DB is updated).

Some other nice features are full text searching, query/result caching, magic finders ($table->findOneByName), etc. Doctrine is build in a logical way, the pragmatic use of ActiveRecord mixed with the power of Hibernate.

So be honest, I someone would tell you all the above, wouldn’t you just make the switch immediately?!?