Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Does Doctrine Add Above Active Record - CodeIgniter?

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

like image 827
MatthewSchenker Avatar asked Jan 12 '12 16:01

MatthewSchenker


People also ask

What is Active records in CodeIgniter?

Active Record Class CodeIgniter uses a modified version of the Active Record Database Pattern. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action.

What is Active Record in Ruby?

1 What is Active Record? Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.


2 Answers

First of all, what Doctrine are you talking about, 1 or 2 ?
There is a huge difference. The only thing that the two have in common is that they are both full-fledged ORM-s. Otherwise there really isn't any connection between the two.

Doctrine 1 is based on ActiveRecords, Doctrine 2 is based on Data mapper pattern.
Both can do same things, but there are some significant differences between the two.

Generally speaking Data mapper is less "developer-friendly" but should have better performance. Why? Actually it is pretty simple. With active records each entity knows everything "around" itself, relation with other entities etc. With data mapper, entities are dumb and lightweight, there is a central entity (EntityManager/UnitOfWork in Doctrine2) which handles all the relation mapping. So in terms of memory usage and performance Data mapper should be faster.
The Doctrine guys say that Doctrine2 is a least 50% faster that Doctrine1 (there are other differences too, not just the design pattern).

If you feel up for it, you can even implement ActiveRecords over Doctrine2 data mapper. Look at this blog post. I'm using this approach just for the development phase, to keep as little code as possible. Once it gets into production I will kill the additional ActiveRecords layer, and rollback to the default data mapper of Doctrine2.

So the conclusion is that you can do everything with both, but in the same way you could say that you can do everything with raw SQL. If you are a beginner in the ORM world, I would suggest going with ActiveRecords, because it is simple and (usually) requires less code. On the other hand, if you are building a large, complex model, I think data mapper is the better option.

Maybe I got something wrong, but this is how I understood it.

As for the comparison between CodeIgniters ActiveRecords and Doctrine (1 or 2), I can't really say, because I never used CodeIgniter. One thing I am sure of, Doctrine has a lot more features than CodeIgniters default ORM. For example: result hydration, inheritance (single table, class table), prefetching, lazy loading, extra lazy loading, extensions, behaviours, optimization, proxies, datetime handling... It is a massive and full-fledged ORM with a lot of features, while my experience with any "default framework ORM" is that their main goal is to be simple as possible, so a newbie can get a hang of it very easily. Doctrine is a mighty beast, and for sure can do a lot of things in a more efficient and/or logically more correct way than the built in CodeIgniter ORM. The downside is, that it takes more time to learn and code, and it is a huge library, with thousands of files, so just to get everything running adds some overhead compared to a lighter alternative.

like image 87
ZolaKt Avatar answered Sep 30 '22 10:09

ZolaKt


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:

class User_model extends CI_Model
{

    public function get_user_by_username($username)
    {
        // Build query using active record methods
        $this->db->where('username', $username);
        $this->db->where('active', 1);

        // Execute query
        $query = $this->db->get('users');

        // Return results
        return $query->result();
    }

    // ...

}

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:

/** @Entity */
class User
{

    /** @Column(type="integer") */
    private $id;

    /** @Column(length=50) */
    private $username;

    // ...

}

Then to use the model and the associated active record functionality, you would do something like this:

// Instantiate object
$user = new User();

// Set properties
$user->username = 'some_username';

// Save object
$user->save();

// Access properties
echo $user->id;

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.

like image 40
birderic Avatar answered Sep 30 '22 10:09

birderic