• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to split the controller / model workload

[eluser]Dave Stewart[/eluser]
Hi All,

I've some questions about the best way to structure classes in order to be flexible, and true to the MVC pattern, within the way that CI likes to work.

So, here's what I'm doing.

1 - I've a bunch of tables, including users, services, products, invoices, etc.
2 - A user can select any one of a number of report types, each of which will call up a fairly different SQL multiple join, returning different-sized and structured datasets
3 - Next, a table is created (although this could be a graph) and it is handed to the view
4 - Additionally some JavaScript related to the table is handed to the view (to allow such things as sorting etc)
5 - Depending on the report chosen, there may also be other HTML options that will be sent to the view

In summary:
1 - report type
2 - data
3 - view
4 - javascript
5 - further options

So, questions:
- Should I build a single model per report, or a master model with a function per report?
- What should the model contain: only database functionality, or the functionality for the JavaScript and HTML as well, or should this be in a controller?
- or should I just be including the actual javascript and such like in custom views per report?

As you can see, I'm not sure where to put everything in order to retain the MVC spirit.


What I feels..

Why should we use models.
For some common database related functions.It saves time and space.and also for big database queries.

Controller -> if you have less code and non repetitive go ahead and use controller as model even as view.

but for large code put each code in separate view file.

Models - mainly for database related queries.
view - contain HTML ,CSS, Javascript

Hope this helps youSmile

[eluser]Dave Stewart[/eluser]
Thanks for your input.

I agree that perhaps the view would be the best place for the JS and HTML. However, I do like the idea of having one class that keeps EVERYTHING to do with a report in one place, rather than splitting it up all over the place.

For example, for each table that gets generated from the model data, I need to send appropriate column formatting data to the HTML view, to be post-processed by JavaScript. It seems logical to me to include all this data within one class, so for example one class / model per report stores:

- SQL query
- table formatting
- Related reports / next options

More views are very welcome, thanks.


Any idea how can you achieve this?

On second thought you love MVC structure right but you don't want file in that format rather you are going for normal php structure.. single... isn't...

[eluser]Dave Stewart[/eluser]
I think maybe the solution might be to abstract my data, and instead have the column formatting data be a property of the model, that gets generated at the same time as the query result.

This way, the controller can grab this data and pass it to a function in the view, which can then act upon it accordingly.

The controller could also make decisions about the "html options" part I was discussing as well, and that way I don't need to muddy the model with data it should not be concerned about.

This looks like we need to create a new CI isn't ..lol

anyways idea is nice just work on it. Smile

First of all html and js should be in the view files. This is why you are having trouble creating a graph. If you keep html out of the models and controllers you can return every formating possible in your views. If you want to make it easy on yourself you can create helpers to generate html for you.

The way i would do it is to have controller method(s) that accepts parameters and based on the controller method the model outputs the data needed to create the content of the view.
I'm not sure how complex your reports are but to make it easier on the user you could create a few report formats based on the barebones report. Lets say you have following urls for the reports users/invoices (show all invoices per user), users/products (show all products per user), invoices/user/1, products/user/1, invoices/products (show products that are attached to invoices). None of them have javascript functions. And say the javascript funtionality is sorting and filtering. Then you can add segments javascript/sort|filter. javascript/sort/filter can be shortened to javascript/all.
In your controller you check if javascript functionality needs to be added or not and you add a link to the js file that has the demanded functionality.
function invoices_users()
   if($this->uri->segment(3) == 'javascript' && $this->uri->total_segments() > 3)
      $content['javascript'] = $this->load->view('jslink',array('filename'=>implode('.',array_slice($this->uri->segment_array(),4))),TRUE);
The javascript files will be named sort.js, filter.js, all.js (joined sort and filter file)

The same thing with the parameters you can use to select the the output. the method would be
function invoices_users()
   $add_javascript = array_search('javascript',$this->uri->segments());
   if($add_javascript !== FALSE && $this->uri->total_segments() > $add_javascript+1)
      $content['javascript'] = $this->load->view('jslink',array('filename'=>implode('.',array_slice($this->uri->segment_array(),$add_javascript+1))),TRUE);
   $output = array_search('output',$this->uri->segments());
   if($output === FALSE  OR $this->uri->segment($output+2) === FALSE)
         case 'graph':
Of course there should be more checks for better security but you get the idea i think.

[eluser]Dave Stewart[/eluser]
Hey xwero,

Thanks so much for the information, and for taking the time to actually write some PHP code. You've really helped to clarify for me where the logic should be going.

I was having similar thoughts myself with regards to views and entering the JS using $this->load->view().

With regards to the sorting, this is my latest reckoning:

1 - Model: build the query and calculate the sort / datatypes
2 - Controller: build the table, and grab the datatype information from the Model, embedding it in the table head th tags: <th datatype="date">
3 - View: have the JavaScript table sorting function be inserted using the Controller->load->view method

The advantage of including the datatype in the table heading is that my Javascript can just look in one place to formulate the sorting algorithms.

Thanks again for your help,

xwero is really nice coder Smile

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.