What does Doctrine Add Above Active Record - CodeIgniter do?

I really like CodeIgniter Active Record and how well it supports all my necessary database queries.

But I also read about ORM, like Doctrine. When I read the Doctrine documentation, itโ€™s not so clear how to use Active Record, and I donโ€™t see what makes it better (if any).

What makes Doctrine impossible with Active Record? Does the Doctrine refine the same work faster, easier, better? Or is he doing what Active Record cannot do?

It would be best if people could publish examples of tasks showing what we are talking about.

Thanks Matthew

+6
source share
2 answers

Doctrine is a complete ORM that implements an active recording template. The CodeIgniter Active Writing Class is a query / database builder based on a "modified" version of the template.

Disclaimer: I have never used Doctrine. I will try my best to illustrate the differences between implementing the active CodeIgniter and Doctrine entries, based on my understanding.

Using the CodeIgniter active record class, you can implement this model:

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(); } // ... } 

Basically, you create a query using active recording methods. It is easy to see how each method ( where() , get() , etc.) maps to the original SQL. The advantage of using active write 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, implementing an active CodeIgniter record doesn't really do much. Any queries you need you need to create. I hope I illustrated how active writing methods look like a query builder.

Please note 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 its associated active recording functions, 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; 

It just scratches the surface in terms of what Doctrine can do. You can set default values โ€‹โ€‹for properties or specify relationships between tables. Please note that I did not write SQL or create a query. I simply set the properties of the object and then saved it. The doctrine takes care of everything else.

Note that Doctrine includes its own query builder, so it does what Active CodeIgniter does, and more.

Using Doctrine is similar to implementing CakePHP or Ruby on Rails active record template. You can look there for more information about the template. CakePHP examples can be especially easy to digest if you come from the CodeIgniter background.

To answer some of your other questions, I donโ€™t think there is anything that makes Doctrine better than CodeIgniter's active write methods. It may be more perfect, but, like any other library, you want to choose the best tool for the job. If you are satisfied with the active CodeIgniter writing methods and donโ€™t see the need for an advanced ORM, skip it.

+9
source

First, what doctrine are you talking about, 1 or 2?
This is a huge difference. The only thing they have is that they are both full-fledged ORM-s. Otherwise, there is no connection between them.

Doctrine 1 is based on ActiveRecords, Doctrine 2 is based on the Data mapping template.
Both can do the same, but there are some significant differences between them.

Generally speaking, the Data Mapper is less "developer friendly", but should have better performance. What for? This is actually quite simple. With active records, each entity knows everything "around" itself, its relationship with other objects, etc. Using a data transfer, entities are dumb and light; there is a central object (EntityManager / UnitOfWork in Doctrine2) that handles all relationship relationships. So, in terms of memory usage and performance, the Data Mapper should be faster.
Doctrine guys say that Doctrine2 is 50% faster than Doctrine1 (there are other differences, not just the design pattern).

If you feel this, you can even implement ActiveRecords on the Doctrine2 data mapper. Take a look at this blog post . I use this approach only for the development phase to save as little code as possible. Once it starts working, I will kill the extra ActiveRecords layer and roll back to the default data collector Doctrine2.

So, the conclusion is that you can do everything with both, but you can also say that you can do everything with raw SQL. If you are new to 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 the data mapper is the best option.

Maybe something is wrong with me, but I understand.

Regarding the comparison of CodeIgniters ActiveRecords and Doctrine (1 or 2), I cannot say because I have never used CodeIgniter. One thing I'm sure of, Doctrine has a lot more features than ORM CodeIgniters. For example: hydration of the result, inheritance (separate table, class table), prefetching, lazy loading, additional lazy loading, extensions, behavior, optimization, proxies, data processing per day ... This is a massive and full-fledged ORM with a lot of my experience working with any "ORM Framework by default" is that their main goal is to be as simple as possible, so a beginner can easily handle this. The doctrine is a powerful beast, and it can certainly do many things in a more efficient and / or logically more correct way than the built-in ORM CodeIgniter. The disadvantage is that it takes more time to learn and encode, and it is a huge library with thousands of files, so just to run it all adds some overhead compared to a lighter alternative.

+18
source

Source: https://habr.com/ru/post/905839/


All Articles