I am relatively new to Joomla and the way they implement an MVC arcitecture. It is slighty different to how I am used to it (Code Igniter, etc) and I am having a few issues simply passing data to the view.
I have created a function in the controller called 'getInitClubs' which sould be run automatically when the page runs, therefore I have made a call to this function from the 'view.html' file.
Here is my code:
Controller function:
public function getInitClubs() {
$model =& $this->getModel('directory');
$init_clubs = $model->clubResults($conds = array());
return $init_clubs;
}
Model Function:
public function clubResults($conds) {
$query = "SELECT * FROM vyj20_contact_details";
if(isset($conds['city'])) {
$query .= " WHERE state = '" . mysql_escape_string($conds['city']) . "'";
}
if(isset($conds['suburb'])) {
$query .= " AND suburb = '" . mysql_escape_string($conds['suburb']) . "'";
}
$this->_db->setQuery($query);
$results = $this->_db->loadObjectList();
return $results;
}
Now i know the model and controller code works, this is not the issue, the issue is actually pulling the results from the controller function 'getInitClubs' and showing them through the view.
How do I call the controller function from the view (like I would on Code Igniter MVC) in order to collect results for display? I have tried and tried but just can't seem to figure it out! Thanks
The JView
instances has getModel()
method. You get the model instance you access a static registry, which is shared between all components. In the JController
instance you should change the state of that model and in the JView
instance you should pulling out the data.
Views in MVC design pattern are supposed to acquire information from model layer and render it using templates or, in some cases, just send a simple HTTP header. In this aspect Joomla has a surprisingly good take on MVC pattern.
P.S. since PHP 5.0 was released (some time around 2005th), you do not need to use references when assigning objects. It is actually harmful, because it messes with refcount.
You controller function should be like.
$model =& $this->getModel('directory');
$init_clubs = $model->clubResults($conds = array());
$view = $this->getView('view_name', 'html'); // get the view
$view->assignRef('data_list', $init_clubs); // set the data to the view
$view->display(); // show the view
Read more
Developing a Model-View-Controller Component