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?
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?
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?
There are several reasons you might choose to use an ORM, a few I can think of:
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.
For database independence (and performance) use PDO. It's built in, its prepared statements are great for preventing SQL-injection vulnerabilities.
If you're stuck on O/RM, it even comes with a little OO-flair... See PDOStatement::fetch (and some of the other fetchXxx methods).
ORM is kinda waste of time. Which is wierd, since it was designed to save you time :) but it is true. I had the oporrunity to use ORM in several languages and it isn't worth it. There are basically 2 types of projects:
Complex projects with many connected tables, views and other stuff. Possibly more databases as well.
In my opinion ORM is alive only thanks to frameworks which use it internally. And performance? I am not sure (since I dont use it anymore) but I believe performance could be a problem too. You have to pay at least some price for the fact that you are abstracted from database query right? I think ORM was great idea on paper but it just doesnt work well.
Using an ORM alleviates the redundancy of writing boilerplate code related to opening a database connection, pooling connections, preparing a statement, executing the statement, mapping the results of executing the statement to something more meaningful, and lastly closing the connection.
It's much cleaner to do something like instantiate an object, set its properties then invoke a save operation on the object. This kind of value alone is worth using an ORM over manually coding the many steps required to interact with a database.
Also, with the a clean ORM it is possible to swap databases with minimal change to the application.
Hope that helps.
What if your server runs PostgreSql, or SQL Server - two other very common database engines.
What if you want to connect to a legacy database?
Also, have you ever maintained any large projects that connect directly to the database? It makes me shiver when I open up a file full of mysql_query
calls.
Performance-wise... Depends on the ORM in question. I'm more familiar with them in a .NET setting, and certainly in that environment there is a wide variance in performance, but this is usually a trade-off with features.
Using an ORM will increase the orthogonality between your code and the database - this will make it easier to develop and maintain. The ability to switch between databases is a benefit of this, but certainly not the sole reason.