Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use an ORM in PHP?

Tags:

oop

php

orm

I recently start studying ORM, a question jumped to my mind:

PHP applications use mostly MySql and Sqlite, almost all PHP servers have that installed, so is it worth to use ORM in PHP to be database-indipendent?

What about performance?

like image 597
Feulf Avatar asked Apr 08 '11 08:04

Feulf


People also ask

What is the point of using an ORM?

Using an ORM will remove dependencies from your code on a particular SQL dialect. Instead of directly interacting with the database you'll be interacting with an abstraction layer that provides insulation between your code and the database implementation.

Is it worth using ORM?

You should try both using ORM and working without ORM with plain SQL. You will see that 99% [1] of time you are better off with ORM. There are very few projects which are so simple that using ORM is not beneficial.


2 Answers

database independence is not the main reason for using an ORM. what you want to have is a general abstraction of the database. this might imply: simplicity of use, faster development, database independence, ... in any case, usually it's worth using an ORM. if your application sucks up all your cpu power, then you might use some plain sql to optimize certain queries. I guess this is a rare case, though.

you might want to read this: What are the advantages of using an ORM?

like image 140
duedl0r Avatar answered Oct 05 '22 23:10

duedl0r


There are several reasons you might choose to use an ORM, a few I can think of:

  • Most ORMs will enable you to validate data for INSERTs and UPDATEs.
  • ORMs will let you map column names in the db to working field names in your db. Which can be handy if you're working with a database that someone else designed and has strange column names.
  • They will also handle relationships nicely for you too. (eg if you fetch a row which has a 1:m relationship on one of the columns you would get a subarray of the related items without having to manually do another query).

Most of the reasons for using an ORM relate to speed/ease of development.

In terms of performance, I've used doctrine with php before and hydration created a huge overhead compared to just fetching the rows.

like image 22
Twelve47 Avatar answered Oct 05 '22 22:10

Twelve47