Internet Bandaid   [RSS Feed]

No SQL in the controller – Best Practice

without comments

Just reviewed someone’s code and I saw this

class Controller
{
function load()
{
    $result = $this->db->query("SELECT * FROM tbl_automobiles");
    $this->view->load('view.php',$result);
}
}

My Response was:
in your controller I see SQL statements. Generally, SQL statement should belong entirely in the model. Anything related to the database or grabbing data, keep that all in the model. So what you could do is Extend your db manage and call it something like AutomobileDbManager, and then have a function like getList() that has your SQL queries. Another good reason to do this is that in larger applications, you have a specialized person working on each tier of the application. You have a DB expert working only on model, and he should not have to touch your controller code. The PHP developer doing purely controller work. And front end person (or even data entry person) do only html in the view. A PHP developer should not have to see a single line of sql code, because he/she may not know it.

Written by John Lai

March 16th, 2014 at 1:38 pm

Posted in Uncategorized

Start Mission - Project management and invoicing

Leave a Reply