简体   繁体   English

将模型数据从控制器传递到视图joomla 3.1

[英]pass models data from controller to the view joomla 3.1

How do I pass properly the models data to the view from controller in joomla 3.1. 如何在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 使用以下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;
}

than if I try to access the model in my view is returning null 比如果我尝试在我的视图中访问模型返回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;

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;

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. 可以在views / somefolder / view.html.php中调用模型函数,从那里可以在模板默认文件中查看assignes变量。

class MyPageViewMyPage extends JViewLegacy { class 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);
}

} }

in the default template file 在默认模板文件中

echo $this->msg;

print_r($this->data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM