How Eloquent in Laravel is different from Doctrine in Implementing ORM

Laravel as one of the most beautiful PHP application framework has a very useful tools in its method of managing database that separating database from other element in the MVC approach. The pattern was known as active record pattern facilitates the creation and use of business objects whose data requires persistent storage to a database.  While ORM is a technique to apply active record in programming. ORM as a software architecture model that stores object data reside in memory into database and work is an M in the MVC model.

Doctrine As Predecessor of PHP ORM

One of the best implementation of Model in MVC for PHP is Doctrine, comprise of PHP libraries to provide access and persistence service to database. Doctrine is able to write database query using its object oriented SQL called DQL, or Doctrine Query Language. DQL enable developers to generate class as an object from existing database and specifying relationship between the object and adding another customized functionality. Therefore, developers do not need to create an XML database schema to create a Model. Doctrine with its lightweight implementation is directed to improve and maintain website performance to handle high-load visits regardless of sudden higher increase of visitors. Most of the PHP application frameworks like CakePHP, CodeIgniter, Symfony and even the heavyweight one Zend use Doctrine as their ORM.

One issue with the Doctrine is the existence of service class called Entity Manager which appeared in Doctrine 2.0 since 2010. Some pros and cons regarding Entity Manager is about its authority to deal with persistence logic in the Doctrine. It is because Doctrine is just PHP libraries which do not know or care of dealing with persistence connection. Therefore it needs a tools to handle persistence connection to database, which came up as Entity Manager that functioned as service class. This consider a genius implementation of ORM for database transaction in PHP, yet it pointed out the lack of ORM implementation in PHP. The insufficiency which filled with Eloquent.

Eloquent in Laravel

The Eloquent ORM is Laravel implementation of ActiveRecord that provides a beautiful and simple method to work with database. Within Eloquent, every table in database possess their own corresponding "Model" to interact with their own table.

As an integrated ORM, Eloquent works nicely to fit in the Laravel and providing a set of command and various method to work with database. It works as more advanced PHP implementation of ORM integrated in the frameworks.

Difference Implementation Between Eloquent and Doctrine

1. Class Implementation

Class implementation in Eloquent and Doctrine is different because Doctrine is simply a plain PHP code calling its ORM libraries, while Eloquent has its own set of class to represent a table in database.
Initiating a class in Eloquent is as simple as this:

class Namelist extends Eloquent {
}

We created a class called namelist as an empty table as we extend the class with Eloquent.
In order to create a table, we use its Schema Builder.

Schema::create('Namelist', function($table)
{
    $table->increments('id');
    $table->char ('name', 4);
});

While in Doctrine we need to call Doctrine with use operator and define its class as a table and create the table  as well.

<?php
use Doctrine\ORM\Mapping AS ORM; 

class Namelist
{
private $id;
private $name;
}
?>

2. Managing Transactional Database

Laravel has a beautiful method to deal with transactional in database. We can just use its transaction method

DB::transaction(function()
 {
 });

We can also enhance the transaction to manually begin and close the transaction:

DB::beginTransaction();
  DB::table('Namelist')->update(['name' => 1]);
DB::commit();

The above transaction method will not commit the transaction when transaction is failed, and we can also rollback the transaction when transaction is failed.

DB::rollBack();

We can also redirect users with error message when transaction failed:

DB::rollBack();
   return Redirect::to('/form')
        ->withErrors( $e->getErrors() )
        ->withInput();

In Doctrine, since its version 2.0, it uses transactional write-behind to interact with database. The method similar as Hibernate ORM in Java. It is logical considering Doctrine was very much inspired from Hibernate and Ruby’s Active Record as well. This method of write-behind provide a delayed execution from the programming layer to reduce network latency. It also provides an efficient order of writing data to database.

The delayed transactional in Doctrine is executed once Entity Manager calls flush() method.

EntityManager::flush();

Once flush() is called, Doctrine will generate SQL queries to update data using database transaction

Comparison

With this implementation, we found that Eloquent is more advanced than the Doctrine in providing database transaction and furthermore to provide a fully MVC implementation in a separate layer, therefore it eases the coding process for programmers. Giving a more readable and elegant code. However, in a matter of performance, we still need to conduct performance testing.

Share this Article on Social Media

All of my Scripts are ready to customized as per your requirement. Feel free to contact for script customization.

Contact me at discussdesk@gmail.com

"Note : It will be charged as per your customization requirement :)"

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments