0

重複の可能性:
joomla MVC コンポーネントで複数のモデルを使用する方法

J!2.5 チュートリアルでは、ビューにロードされるモデルは 1 つだけです (デフォルトのモデル)。J!1.5 のスニペットは、チュートリアルのテクニックを使用しても機能しなくなりました。これが私のもので拡張されたものです。 controller.php

    function display($cachable = false) 
{
    // set default view if not set
    $input = JFactory::getApplication()->input;
    JRequest::setVar('view', JRequest::getCmd('view', 'HelloWorlds'));

    // We will add a second model "bills"
    $model = $this->getModel ( 'helloworlds' ); // get first model
    $view  = $this->getView  ( 'helloworlds', 'html'  ); // get view we want to use
    $view->setModel( $model, true );  // true is for the default model  

    $billsModel = &$this->getModel ( 'mycomponents' ); // get second model   
    $view->setModel( $billsModel );             

    // call parent behavior
    parent::display($cachable);
}

そして、これが私の見解です。 ビュー/helloworlds.view.hml.php

    function display($tpl = null) 
{
    // Get data from the model
    $model = $this->getModel();
    $items = $this->get('Items');
    $pagination = $this->get('Pagination');

    $model2 = &$this->getModel('mycomponents');
    $items2 = $model2->get('Items');
    $pagination2 = $model2->get('Pagination');

    // Check for errors.
    if (count($errors = $this->get('Errors'))) 
    {
        JError::raiseError(500, implode('<br />', $errors));
        return false;
    }
    // Assign data to the view
    $this->items = $items;
    $this->pagination = $pagination;
    $this->items2 = $items2;
    $this->pagination2 = $pagination2;

    // Display the template
    parent::display($tpl);
}

残念ながら、model2 がロードされていません。何が起きてる?私のエラーはコントローラーにありますか?コントローラで両方のモデルを正しく設定しましたか? そして、ビュー内の私のコードはどうですか? その方法でモデルをロードする必要がありますか (J!1.5 から知られています)?

よろしくビョルン

4

1 に答える 1

0

わかった。解決策は、モデルをコントローラーに登録することです (既に行ったように)。

そして、ビューでそれを呼び出すだけです

    $items2 = $this->get('Items', 'MyComponents');

それ以外の

    $items = $this->get('Items');

ここに私の完全なビューソースがあります:

    function display($tpl = null) 
{
    // Get data from the model
    $items = $this->get('Items');
    $pagination = $this->get('Pagination');

    $items2 = $this->get('Items', 'MyComponents');
    $pagination2 = $this->get('Pagination', 'MyComponents');

    // Check for errors.
    if (count($errors = $this->get('Errors'))) 
    {
        JError::raiseError(500, implode('<br />', $errors));
        return false;
    }
    // Assign data to the view
    $this->items = $items;
    $this->pagination = $pagination;
    $this->items2 = $items2;
    $this->pagination2 = $pagination2;

    // Display the template
    parent::display($tpl);
}
于 2013-01-31T13:18:44.953 に答える