I really like CodeIgniter’s Active Record and how nicely it allows all my needed database queries.
But I’ve also been reading about ORMs like Doctrine. When I read Doctrine’s documentation, it does not seem as clear to use as Active Record, and I can’t see what makes it better (if it is).
What does Doctrine allow that is not possible with Active Record? Does Doctrine make the same job faster, easier, better? Or does it do things Active Record cannot do?
Best would be if people could post examples of tasks showing what we’re talking about.
Thanks,
Matthew
Doctrine is a full-fledged ORM that implements the active record pattern. CodeIgniter’s active record class is a query builder/database wrapper that is based on a “modified” version of the pattern.
Disclaimer: I have never used Doctrine. I will try my best to illustrate the differences between CodeIgniter’s active record implementation and Doctrine, based on my understanding.
Using CodeIgniter’s active record class, you might implement a model like this:
You are basically building the query using the active record methods. It’s easy to see how each method (
where(),get(), etc) maps to raw SQL. The advantage to using the active record methods as opposed to just$this->db->query()is that CodeIgniter compiles each query based on the database driver you are using. Other than that, CodeIgniter’s active record implementation doesn’t really do much. Any queries you need, you’ll need to create. I hope I’ve illustrated how the active record methods are similar to a query builder.Note that that the following sample code may be incorrect. Using Doctrine, you might have a model like this:
Then to use the model and the associated active record functionality, you would do something like this:
This is just scratching the surface in terms of what Doctrine can do. You can set default values for properties or specify relationships between tables. Notice how I didn’t write any SQL or build the query. I just set the properties of the object and then saved it. Doctrine takes care of the rest.
Note that Doctrine includes its own query builder, so in a way it does what CodeIgniter’s active record does, and more.
Using Doctrine is similar to CakePHP’s or Ruby on Rails’ implementation of the active record pattern. You could take a look there for additional insight into the pattern. CakePHP’s examples might be particularly easy to digest if you’re coming from a CodeIgniter background.
To answer some of your other questions, I don’t think there’s anything that makes Doctrine better than the CodeIgniter active record methods. It may be more advanced, but like any other library, you want to pick the best tool for the job. If you are happy with CodeIgniter’s active record methods and you see no need for an advanced ORM, then skip it.