PHP/MVC newb question: How does "OO" fit into "MVC" (simple example)

KevySaysBeNice

[H]ard|Gawd
Joined
Dec 7, 2001
Messages
1,452
Hi all!

I'll (try to) keep this short:

I'm working on a project and I'm finding myself wondering how I should be doing stuff in PHP, and if I'm doing things "MVC" or if I'm doing things "OO", or both, or what. Basically, here is an example to illustrate my point:

I am working on a for-fun "craft" website, that lists craft projects I've worked on. In my views, I am doing things like
Code:
foreach($crafts as $craft)
{
    <img src="<?php echo craft->getMainImage(); ?>" />
}

The problem is, I'm wondering where I should be loading up a "craft" object.

For instance, should I make a Craft class, that contains all of the attributes, getters/setters, etc, for a Craft, and then create a model that basically loads all of the data into a Craft class?

OR, should my Craft class automatically load all of the data itself, so it's 100% self contained. So I do $craft = new Craft(); and a new Craft object is created which automatically (in it's __Construct for instance) does all of the DB stuff to pull in all of the data about the Craft?

Or should I do a combination of the two, so that I can (for instance) do $craft = new Craft(0); - passing in "TRUE" populates the most recent Craft, passing in another ID will populate that crafts ID. So that way, I could do something like (in my model)
Code:
function getCrafts($start_date, $end_date)
{

//query database to get a list of the crafts that were done within the given date range
// store in $craft_id_list

$craft_list = array();

foreach($craft_id_list as $craft_id)
{
$craft_list[] = new Craft($craft_id);
}

return $craft_list;

}


Is this how to do it?

Or should I leave ALL DB stuff in the model, and just create a Craft class for storing the data that I pull out of the model?

<3
 
p.s. Also, I realize that this is really slow probably. I'm not particularly worried about performance as this will be a VERY low traffic site (again, for fun mainly), but I would like to learn and do a good job.

Obviously the example I gave above would make a seperate call for EACH craft object that was being requested, as apposed to pulling back ALL of the data for ALL of the require Craft IDs with a single query.

I guess maybe this is an issue of readability/simplicity vs efficiency? It seems pretty wasteful to make a seperate DB call though for each Craft, considering there might be 10 Crafts on a page, and therefor 10-11 DB requests for a single page load.

Instead, I could simply query the DB and return the rows, and totally forget the Craft objects, and that would work pretty much just as well, but again, if I wanted to somehow validate or further process the data then it seems fitting the data into some sort of object would be a good idea?

Thanks again!
 
Back
Top