2011-03-22 62 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()