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
#2

[eluser]toopay[/eluser]
M stand for Model : class for handle all of your database abstraction
V stand for View : a file which you used for outputing your http response. Ussually just a html structure file with pseudo variable mark-up
C stand for Controller : class for handle all of your logic, which decide the output/result for every HTTP request

Library, is just a class contain reusable or usefull function, which you may need in most of your controller, model, or even in your view.
#3

[eluser]CodeIgniteMe[/eluser]
Just not to confuse any newbies...
[quote author="shoelessone" date="1309387615"]
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.
[/quote]

must be edited into
Quote:going to /site_controller/scores calls site_controller->scores()
//site_controller URI calls site_controller controller
The Site controller’s function scores() loads the site_model, then does something like $data['scores'] = $this->site_model->getScores();
// store the result in an array if you want to pass it to your view
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.
#4

[eluser]CodeIgniteMe[/eluser]
This could be a case of shifting to the Hierarchical Model-View-Controllers. In which, you can have Modules of MVCs. This will simplify your application into small bits of totally reusable codes. You can have multiple MVCs which may represent each High school Math Clubs wherein they have their own Model/s, View/s, and Controller/s (or even use the MVC of the other Math Clubs), and the member class could be just a module that every MVC can call. Please refer to the Modular Extensions HMVC (https://bitbucket.org/wiredesignz/codeig...sions-hmvc)

Anyway, I don't have sufficient info about this, because I'm just a newbie on HMVC.




Theme © iAndrew 2016 - Forum software by © MyBB