The Last PHP PDO Library You Will Ever Need
leftnode.com
leftnode.com
You don't want to write SQL in your controllers. It's a recipe for future pain and suffering.
- If a second action needs the same data, it's likely that you'll end up duplicating your query. Especially if you work with other people, and they don't know about the queries that exist in all of the different actions.
- If you change the structure of the data in the DB, you need to find every affected query, in every action. It's extremely fragile, and prone to breaking.
If you have a model, with a nice public API, that interacts with the DB, you know that you've isolated the change to just that one place. All of the controllers that call $model->some_data(); will continue to work no matter how your change your data source so long as you obey the API.
There are million different ways you can approach that, but I strongly recommend that you find one that works for you, and stick with it.
I think part of the reason for this is it appears in allot of tutorials for different web frameworks, which I think they do because it makes the code smaller and therefor their framework look simpler.
I see that this has been submitted before, so I'll just mention it here: http://thoughts.j-davis.com/2011/09/25/sql-the-successful-co...
I found it with hnsearch, by the way.
But doing your SQL Statements in the Controller is just not the way to go.. What if you need the same query on different places?
What I like about good ORMs (like Doctrine or the ActiveRecord of RoR) is the migration part. Maybe there should be a tool which only supports you in creating the tables and handling migrations.
Lately I've been using RedBean ORM for my projects. It lets you write your SQL (no query builders or anything stupid), but creates tables, columns, and foreign keys on the fly.
Here's some RedBean code from one of my projects:
- If you do quick ad hoc script/page - use straight sql with mysql_query
- If you extend it - use something like PDO
- If you build real application, you have to use Model layer. And no matter what you prefer at this point - you'd better to put everything behind Model be it PDO-based queries, some dynamic objectish query builders or full-scale ORM like DBIx::Class (sorry, this is for Perl, not very familiar with something like this in PHP. If someone know good analogs in PHP - please post links to these).
Right tool for right task - this is what important to keep in mind.
PS: Sorry, but seeing straight SQL in what looks like an action, is really hurt my eyes. If you opted for MVC framework, you'd probably want to push all SQL into model and leave meaningful calls to it.
Ill take a messy system with hand coded SQL over that any day, atleast then you have a chance to refactor.
Also, its not that hard to implement a pluggable data access layer (that uses hand written SQL). The most important thing is to keep your RDBMS data model separate from your application model. These two will be intermingled when you are using a ORM, in my experience.
I like a basic Model class with a few helpers that I can extend to keep my logic separated from my controllers, but that's about it. Inside any given Model method, SQL is fair game.
It feels natural, also it let's you do SQL if you really need it.
Maybe it's just me. :)
For me it also means anything that tries to generate SQL.
Some rearranging/rewriting would make it easier to parse. E.g.:
“In this article I will use the term ORM to refer to any library or framework for interacting with a database; that is, to refer to anything other than writing straight SQL.”
$total_users = $db->fetch_scalar("SELECT COUNT(*) FROM users");
I'm not saying there aren't downsides to ORMs, but the counter-arguments to SQL in your controller are pretty obvious.
I also would have gone with naming consistent with PDO's (i.e. `halfCamelCase` rather than `lower_case`), but that's personal preference. Consistency would be nice though.