Welcome to Delicate template
Header
Just another WordPress site
Header

Why I switched to Doctrine

March 20th, 2008 | Posted by Michaël in ICT | Symfony | Web Development - (5 Comments)

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 heared 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 allready 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 Doctine 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$c = new Criteria();
 
$cton1 = $c->getNewCriterion(AuthorPeer::FIRST_NAME, "Leo");
 
$cton2 = $c->getNewCriterion(AuthorPeer::LAST_NAME,
array("Tolstoy",  "Dostoevsky", "Bakhtin"), Criteria::IN);
 
$cton1->addOr($cton2);
 
// add to Criteria
 
$c->add($cton1);
 
$authors = AuthorPeer::doSelect($c);

with this Doctrine code:

1
2
3
4
5
$authors = $conn->query("
 
FROM Author a WHERE a.first_name = ? OR a.last_name IN ?
 
",array('Leo',array("Tolstoy", "Dostoevsky", "Bakhtin"));

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 smoothley 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 usefull 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 )

Doctine 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 honost, I someone would tell you all the above, wouldn’t you just make the switch immediately?!?

Switching to Doctrine

March 18th, 2008 | Posted by Michaël in ICT | Symfony | Web Development - (0 Comments)

This weekend, I decided to give Doctrine a shot. Since Propel is the default ORM layer, I never really looked at Doctrine before. But reading on the subject got me interested. Luckely their is allready a nice Doctrine plugin available for Symfony, so the switch went smooth, without any problems.

I have to admit, that I was losing my mind in the beginning. I had to get used to the new syntax, and all the new possibilities of Doctrine just blew me away.

I made a backup of a running project developed in Propel (1.2) and just started refactoring the schema and chaning the code. It’s been allmost 5 days, and I wouldn’t go back to Propel at all. Even for the 1.3 version (where people say that it is even faster then Doctrine) . There are plenty of reasons why to stick with or switch to Doctrine, and to be honest, even those slightly speed increases of Propel 1.3 just can’t compete with them.

For those that are still not sure, just check out the Doctrine Documentation. You will find everything you want,  with some nice examples. And for some implementations in Symfony, you can download a Doctrine version of Askeet somewhere on the forum (I know it helped me a bit).

The project is going along nicely. I can’t say mutch yet, but I’ll keep you informed when it is  allmost finished. Their is still a lot of work to do, but I’m getting their, and with the help of Symfony and Doctrine, it just can’t go wrong.

Well, it’s been getting late, and after spending 15 hours behind the computer, I want to spent a few hours before the tv screen to relax.

I’ll explain later in detail what made me switch to Doctrine. In the mean time, let me know what you think about Doctrine or Propel and why you are using that specific ORM.