Welcome Guest, Not a member yet? Register   Sign In
Newb question: instances of classes vs models?
#1

[eluser]shoelessone[/eluser]
Hi all!

So, I've got what I'd consider a serious newb question that will show my lack of understanding of fundamental MVC design pattern. I'd really appreciate any advice or direction on "best practices."

I started out learning CodeIgniter and MVC framework "stuff" in general from the very excellent tutorials on nettuts.com, which helped me a ton. Soon however, when working on my own projects I found myself confused about where models and instances of classes meet up. Let me give a simple example:

Let's use an example of a high school math club's website (hypothetical, of course!)


In the beginning, I might have started with a simple controller, call it site_controller, and a simple model, site_model. I might only have the single controller, which basically just calls the proper model method to get data for the view. So for example, on a page that shows the math club's latest results, I might have something like:

going to /site/scores calls site_controller->scores()
The Site controller's function scores() loads the site_model, then does something like $scores = $this->site_model->getScores();
The site controller then passes $scores to the view (say scores_view.php), which loops through the $scores and prints out a list of the sites scores.

So that worked pretty well I guess. This is a pretty simple example of course. I might also have had a User model, so I could do stuff like if($this->user_model->active()) for instance.


BUT NOW, fast forward a while, and I find myself working on more complicated hobby projects. For instance, maybe I have a website for all of the New York High School Math Clubs. Maybe each club has it's own page, and each club has a collection of students. NOW, I find myself creating Libraries (here is where the question really comes in, is a library the correct thing to use?), for instance I have a Club class, and a Member class. Rather then calling a site model (or even club_model->getScores()) to do something like getScores(), I do something like:

$userClub = new Club(TRUE);

Inside of the $userClub constructor, I might check to see if there is a current user logged in, and if so and "TRUE" is passed in, then I might do something like:
$this->setID($user->getUserClub());
$this->load();

the load method, within the Club class might handle of of the SQL/query "stuff" to get the Clubs latest events, stats, it might load an array (say $this->members) with another set of Member objects, each Member representing a member of the club, etc. The bottom line is that all of the SQL and much of the logic would be self contained in the Club/Member/etc "libraries", totally bypassing the idea of a model all together. So most of my heavy lifting ends up being done in my stand alone library classes, not my models. In many cases I can pretty much totally bypass any sort of Model. In the instance of a club homepage, my controller can do something like $clubInfo = new Club(TRUE); and I send the $clubInfo object to the view.


So, the question is, what am I doing wrong? What should I be doing differently? Are these things Libraries, or should they somehow be Models?

Thank you SOO much for reading all of that, hopefully it made some sense and you might be able to help me become a better developer!

Happy day!
Kev


Messages In This Thread
Newb question: instances of classes vs models? - by El Forum - 06-29-2011, 11:46 AM
Newb question: instances of classes vs models? - by El Forum - 07-01-2011, 02:17 AM
Newb question: instances of classes vs models? - by El Forum - 07-10-2011, 07:53 PM
Newb question: instances of classes vs models? - by El Forum - 07-10-2011, 08:11 PM



Theme © iAndrew 2016 - Forum software by © MyBB