如何在joomla 3.1中将模型数据正确传递给控制器中的视图。在开始时,我初始化我的一个子控制器方法,以收集应该填充我的表单布局的项目的数据。使用以下URL ?option=com_unis&task=unis.edit&layout=edit&id=1
访问,而不是我的控制器方法
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;
}
比我在我的视图中尝试访问模型时返回null
找到了!但也许不是最好的解决方法
在视图中我初始化我的模型
$model = $this->getModel('mymodel');
$data = $model->my_method($args);
与使用公共变量
的布局相关联$this->data = $data;
答案 0 :(得分:1)
毕竟我找到了解决方法。在视图中,我按照以下方式调用我的模型
$model = $this->getModel('mymodel');
$data = $model->my_method($args);
我创建了一个保存布局数据的公共变量
$this->data = $data;
答案 1 :(得分:1)
控制器选择要使用的视图。可以在views / somefolder / view.html.php中调用模型函数,从那里可以在模板默认文件中查看assignes变量。
类MyPageViewMyPage扩展了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);
}
}
在默认模板文件
中echo $this->msg;
print_r($this->data);