API16

JController/getModel

From Joomla! Documentation

< API16:JController

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Method to get a model object, loading it if required.


<! removed transcluded page call, red link never existed >

Syntax[edit]

getModel($name= '', $prefix= '', $config=array())
Parameter Name Default Value Description
$name The model name. Optional.
$prefix The class prefix. Optional.
$config array() Configuration array for model. Optional.

Returns[edit]

object The model.

Defined in[edit]

libraries/joomla/application/component/controller.php

Importing[edit]

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

Source Body[edit]

function getModel($name = '', $prefix = '', $config = array())
{
        if (empty($name)) {
                $name = $this->getName();
        }

        if (empty($prefix)) {
                $prefix = $this->getName() . 'Model';
        }

        if ($model = & $this->_createModel($name, $prefix, $config)) {
                // task is a reserved state
                $model->setState('task', $this->_task);

                // Lets get the application object and set menu information if its available
                $app    = &JFactory::getApplication();
                $menu   = &$app->getMenu();
                if (is_object($menu))
                {
                        if ($item = $menu->getActive())
                        {
                                $params = &$menu->getParams($item->id);
                                // Set Default State Data
                                $model->setState('parameters.menu', $params);
                        }
                }
        }
        return $model;
}


<! removed transcluded page call, red link never existed >

Examples[edit]

Get and load a model with the same name as the current view

   $model = &$this->getModel();
   /* Or you could use
   $model = JController::getModel();
   */

   // Model is an object, so call a function
   $results = $model->myfunction();

Load a Model by name

   $model = &$this->getModel('foo');
   /* Or you could use
   $model = JController::getModel('foo');
   */

   // Model is an object, so call a function
   $results = $model->myfunction();

Code Examples[edit]