Question

How do I pass properly the models data to the view from controller in joomla 3.1. On start I initialize one of my sub controllers method to gather data on item which should fill up my form layout. Is accessed with the following url ?option=com_unis&task=unis.edit&layout=edit&id=1 than my controllers method looks like

public function edit() 
{
    $input = JFactory::getApplication()->input;     
    $model = $this->getModel ( 'item'); 
    $view = $this->getView('item', 'html');   
    $view->setModel($model);
    $view->setLayout('edit');

// Display the view
$view->display();

return $this;
}

than if I try to access the model in my view is returning null

Found it! But maybe is not the best workaround

in the view I init my model like

$model = $this->getModel('mymodel');
$data  = $model->my_method($args);

than associate to the layout with a public variable

$this->data = $data;
Was it helpful?

Solution

After all I found out the workaround. In the view I call my model as it follows

$model = $this->getModel('mymodel');
$data  = $model->my_method($args);

than I created a public variable which holds the layout data

$this->data = $data;

OTHER TIPS

The controller picks up the view to be used. The model functions can be called in the views/somefolder/view.html.php from there the assignes variables can be viewed in the template default file.

class MyPageViewMyPage extends JViewLegacy {

/**
 * Display the Hello World view
 *
 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
 *
 * @return  void
 */

public $data;

function display($tpl = null) {
    // Assign data to the view
    $this->msg = $this->get('Msg'); // call a method msg in the model defined

    $model = $this->getModel(); // creating an object for the model

    $this->data = $model->getFilter(1); // call a method defined in model by passing the arguments
    // Check for errors.
    if (count($errors = $this->get('Errors'))) {
        JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');

        return false;
    }
    // Display the view                
    parent::display($tpl);
}

}

in the default template file

echo $this->msg;

print_r($this->data);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top