2011-03-22 90 views
0

這是我的控制器如何在joomla中加載模型?

// No direct access 

defined('_JEXEC') or die('Restricted access'); 

jimport('joomla.application.component.controller'); 

/** 
* Hello World Component Controller 
* 
* @package Joomla.Tutorials 
* @subpackage Components 
*/ 
class HelloController extends JController 
{ 
    /** 
    * Method to display the view 
    * 
    * @access public 
    */ 

    function __construct($default = array()) 
    { 
     parent::__construct($default); 

     // Register Extra tasks 
     $this->registerTask('detail' ,  'display'); 
    } 
    function display() 
    { 
    switch($this->getTask()) 
     { 
     case 'detail'  : 
     { 
      JRequest::setVar('view' , 'new'); 

     // Checkout the weblink 
      $model = $this->getModel('hello'); 

     } break; 

    } 
    parent::display(); 
    } 


} 

這是我view.html.php

class HelloViewNew extends JView 
{ 
    function display($tpl = null) 
    { 
    global $mainframe; 

    $db  =& JFactory::getDBO(); 

    $model =& $this->getModel('hello'); 

    $items  = & $model->getdetail(); 

    $this->assignRef('items', $items); 

    parent::display($tpl); 
    } 

} 

,這是我的模型

defined('_JEXEC') or die('Restricted access'); 

jimport('joomla.application.component.model'); 

/** 
* Hello Model 
* 
* @package Joomla.Tutorials 
* @subpackage Components 
*/ 
class HelloModelHello extends JModel 
{ 
    /** 
    * Gets the greeting 
    * @return string The greeting to be displayed to the user 
    */ 
    var $_data; 

    /** 
    * Returns the query 
    * @return string The query to be used to retrieve the rows from the database 
    */ 
    function _buildQuery() 
    { 
    $query = ' SELECT * ' 
     . ' FROM #__hello WHERE published = 1' 
    ; 

    return $query; 
    } 

    /** 
    * Retrieves the hello data 
    * @return array Array of objects containing the data from the database 
    */ 
    function getData() 
    { 
    // Lets load the data if it doesn't already exist 
    if (empty($this->_data)) 
    { 
     $query = $this->_buildQuery(); 
     $this->_data = $this->_getList($query); 
    } 
    //echo "<pre>"; print_r($this->_data); exit; 
    return $this->_data; 
    } 
    function detail() 
    { 
    echo "this is test"; exit; 
    } 

} 

我的問題是我怎樣才能獲取該功能的細節從數據庫它不適合我?

回答

1

你的模型,你的功能:function detail(), 但你已經試過來調用視圖功能:$items = & $model->getdetail(); 記住你的功能detail()getdetail()。因此,請在致電:

$items = & $model->detail();

這是你唯一的我想是錯誤的,運氣好的話

+0

+1。我沒有注意到這一點。 – Gaurav 2011-03-22 05:32:33

+0

@Gaurav和@prakash:我試過這個,但是這再次不起作用,實際上在控制器上,模型加載並且在view.html.php中沒有加載。我可以使用setVar和getVar嗎? – Pus 2011-03-22 05:58:21

+0

@Pushparaj Joshi:檢查我的答案。 – Gaurav 2011-03-22 07:37:21

0

你應該在位指示

$view = $this->getView(); 
$view->setModel($this->getModel()); 

使用,那麼你可以考慮使用$this->getModel()