Pus
Pus

Reputation: 799

How can i load model in joomla?

This is my Controller

// 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();
    }


}

this is my 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);
    }

}

and this is my model

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;
    }

}

My question is how can i fetch that detail function from database its not working for me?

Upvotes: 0

Views: 5102

Answers (2)

Gaurav
Gaurav

Reputation: 28755

you should use this in contoller

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

then you can use $this->getModel() in view.

Upvotes: 0

Prakash
Prakash

Reputation: 6602

on your model, you've the function : function detail() , But you've tried to call the function on view with : $items = & $model->getdetail(); Remember your function is detail() NOT getdetail() . So, call with :

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

That's your only mistake I guess so, good luck

Upvotes: 1

Related Questions