Sunday, April 5, 2015

Let's talk in Eloquent ORM | Topic One "Why should you use an ORM?"

I know that most of us used to use the normal way to write SQL Queries , and also they may refused the framework attitude toward database queries. 

There's not much point in using an ORM for following cases :


1-If you aren't dealing with objects.
2-If your relational tables/columns map 1:1 with objects/attributes.
3-If your objects don't have any 1:1, 1:m or m:n relationships.
4-If you have complex, hand-tuned SQL.
5-If you've decided that your database will have stored procedures as its interface.
6-If you have a complex legacy schema that can't be refactored.

So here's the converse:


If you have a solid object model, with relationships between objects that are 1:1, 1:m, and m:n, don't have stored procedures, and like the dynamic SQL that an ORM solution will give you, by all means use an ORM.


Decisions like these are always a choice. Choose, implement, measure, evaluate.

But what are the Benefits of Using Object Relational Mapping ?!



1-ORM frees the programmer from dealing with simple repetitive database queries. Instead of writing SQL, the programmer can request objects from the ORM's persistence layer and call a save procedure after updating an object. Alternatively, some ORM libraries will automatically persist updated objects, such as during garbage collection or system shutdown.

2-Conceptually, automatically mapping the database to business objects lets programmers focus more on business problems and less with data storage.

3-The mapping process can aid in data verification and security before reaching the database. Proper data can be confirmed and appropriate messages can be returned to the user without relying on failed database queries.

4-ORM can provide a caching layer above the database. For example, if the same object is requested multiple times, it's possible for the ORM layer to retain and reuse the same object, rather than re-querying the database.