Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for organizing SQL queries in code?

Tags:

sql

php

I have a bunch of beautiful OOP code but the functions have these monstrously huge SQL queries. In 2010, should we really still be writing our queries in-line or is there an abstraction, like MVC, for SQL queries in PHP?

Thank you for your insights.

Edit: I probably should have mentioned this here. But as a professional PHP engineer, you wouldn't believe how often I've come across ORMs done horribly and (even worse!) ORMs programmed correctly that perform absolutely terribly (esp. on many-to-many and many join queries). I'm very negatively biased towards ORMs, with the exception of a few simple ORMs, like Kohana.

I guess what I was looking for was a generic Dynamic SQL design pattern.

like image 826
Theodore R. Smith Avatar asked Aug 31 '10 19:08

Theodore R. Smith


3 Answers

you can write your queries (select, insert, update, delete,...) in stored procedures and only call those stored procedures from within your php code instead of writing your queries in your php files.

a good resource for learning stored procedures in mysql is this book:

mysql stored procedure programming

like image 68
rahim asgari Avatar answered Sep 19 '22 19:09

rahim asgari


I don't know about PHP, but you could use an ORM. Wikipedia has a nice list of ORM software.

like image 30
nmichaels Avatar answered Sep 23 '22 19:09

nmichaels


You can look into using Doctrine ORM. Not sure if that is what you were asking for or not.

like image 25
Jim Avatar answered Sep 22 '22 19:09

Jim