I am writing code for a dynamic website which stores it's data in MySQL. You can think of it as a custom-built CMS.
It's rather simple. I have classes, such as article, image, slideshow...
Now the question I am struggling with is should i put SQL ...
1 - Inside these classes, example:
function loadId($id) {
$res = mysql_query("SELECT FROM articles WHERE id=".(int)$id);
$articleRecord = mysql_fetch_object($res);
... populate object variables here....
}
2 - Or outside, to have the power of SQL at my displosal. like that:
Inside the calling file... you could call it the view, although it's not a proper view:
$res = mysql_query("SELECT FROM articles WHERE id=".(int)$id);
$article->loadFromRes($res);
Inside the class:
function loadFromRes($res) {
$articleRecord = mysql_fetch_object($res);
... populate object variables here....
}
I really like the second option, but I have a feeling it'll get ugly with related tables, joins, etc...
So, what do you people think?