CodeIgniter Forums
Correct understanding of MVC system? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Correct understanding of MVC system? (/showthread.php?tid=25954)



Correct understanding of MVC system? - El Forum - 01-01-2010

[eluser]JanDoToDo[/eluser]
Hey guys, Im new to using CI and the MVc method and was hoping for some feedback. Ive seen a lot of posts recently about pagination and a few commetns about what code should go in controllers/views/models. I was hoping you coould comment on my approach below and tell me if I've gone wrong. Anyt thoughts would be hugely appreciateD! thankyou!

Function within controller:
function Top_Rated()
{
//Other code not related to the model
// Set all the configureation for the pagianation system
$config['base_url'] = base_url() . '/boats/Top_Rated';
$config['total_rows'] = ($this -> db -> count_all('video_links') > 100) ? 100 : $this -> db -> count_all('video_links');
$config['per_page'] = '9';
$this -> pagination -> initialize($config);

//Load the search model and name it boat_search
$this -> load -> model('boatsearch_model','boat_search');

//Set the boat_results var for the view as the result of get_boats from the boat_search model(gets passed the num per page, where to start and the order)
$page_info['boat_results'] = $this -> boat_search -> get_boats($config['per_page'], $this -> uri -> segment(3), 'rating' );

// continue with code that isnt to do with model
}

The model:
class Boatsearch_model extends Model
{
var $CI;
var $boat_results;
/*
Constructor
*/
function Boatsearch_model() {
parent::Model();
$this -> table_name = 'video_links';
$this -> CI =& get_instance();
}
function get_boats($resPerPage = 9, $resStart = 0, $sorting = 'rating' )
{
$resStart = is_numeric($resStart) ? $resStart : 0;
//Run the query for the pagination
$boat_search = $this -> CI -> db -> query ( "SELECT * FROM " . $this -> table_name . " ORDER BY $sorting DESC LIMIT $resStart, $resPerPage" );
//Get the resultset from the pagination
$this -> boat_results = $boat_search -> result_array();
//Draw the pagination links and the contents of the array as items on the page
$boat_search = $this -> _draw_results();

//return the results from the draw_results page and set this in the controller as the content for the page body
return $boat_search;
}

function _draw_results()
{
$resultsData = $this -> boat_results;
//Complete all the code which draws the pagination links and also draws all of the returned items as HTML and then returns data
return $response;
}

I hope you guys can help and tell me if I'm doing things in a weird way!

thankyou! Smile
}


Correct understanding of MVC system? - El Forum - 01-01-2010

[eluser]Colin Williams[/eluser]
Models provide functions to manipulate data, and Controllers and Views work together to create the interface. Looks like you are trying to use the model to build the interface, so you should rethink your approach. A model should not only never call a view, it should never have a reason to. And this doesn't mean markup will never exist in a model--consider a model that must format data as XML before storing it. This would have nothing to do with the interface though. See the difference?


Correct understanding of MVC system? - El Forum - 01-02-2010

[eluser]David Cassidy[/eluser]
It is also worthwhile to mention that you do not need to use get_instance() within your model. This is primarily for utilizing CodeIgniter resources inside custom libraries and the like. Instead, simply use $this->db. Good luck!


Correct understanding of MVC system? - El Forum - 01-02-2010

[eluser]JanDoToDo[/eluser]
Thanks for your repsonses. Smile So, in my case above, I am using the get_boats to run a query and then return the result set from that query and then running the draw_results to create the markup for the pagination links and the content on the page. Would you suggest taking the draw_results function out of the model and then putting it in the controller then? In which case, all the model would do is run the query and then return the resultset back to the controller?

I put the draw_results function in the model as I want different controllers to haave access to it. How would I do it otherwise..? Oh.. I just thought.. I guess i'd use a library as well??

Many thanks! Smile Oh and Dave_c -> I'll make the correction! Smile


Correct understanding of MVC system? - El Forum - 01-02-2010

[eluser]David Cassidy[/eluser]
[quote author="JanDoToDo" date="1262453882"]Thanks for your repsonses. Smile So, in my case above, I am using the get_boats to run a query and then return the result set from that query and then running the draw_results to create the markup for the pagination links and the content on the page. Would you suggest taking the draw_results function out of the model and then putting it in the controller then? In which case, all the model would do is run the query and then return the resultset back to the controller?

I put the draw_results function in the model as I want different controllers to haave access to it. How would I do it otherwise..? Oh.. I just thought.. I guess i'd use a library as well??

Many thanks! Smile Oh and Dave_c -> I'll make the correction! Smile[/quote]

Relocating _draw_results() to your controller is precisely the way you should do it. If you want to make it accessible to other controllers, a library (or perhaps a simple helper - since a library would be a bit of overkill) would do the trick.


Correct understanding of MVC system? - El Forum - 01-02-2010

[eluser]JanDoToDo[/eluser]
Ok.. Thankyou!!!


Correct understanding of MVC system? - El Forum - 01-02-2010

[eluser]David Cassidy[/eluser]
Keep in mind also, that if you plan to use a helper and need access to CodeIgniter resources, you may need to use the get_instance() function as outlined near the middle of the page here: Utilizing CodeIgniter Resources within Your Library.