API16

JController/getModel: Difference between revisions

From Joomla! Documentation

BenTasker (talk | contribs)
m Examples: Adding a couple of examples
m removing red link to edit, no existant pages
Line 3: Line 3:


<span class="editsection" style="font-size:76%;">
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JController/getModel|Edit Descripton]]<nowiki>]</nowiki>
<nowiki>[<! removed edit link to red link >]</nowiki>
</span>
</span>


{{Description:JController/getModel}}
<! removed transcluded page call, red link never existed >


===Syntax===
===Syntax===
Line 72: Line 72:


<span class="editsection" style="font-size:76%;">
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[SeeAlso:JController/getModel|Edit See Also]]<nowiki>]</nowiki>
<nowiki>[<! removed edit link to red link >]</nowiki>
</span>
</span>
{{SeeAlso:JController/getModel}}
<! removed transcluded page call, red link never existed >


===Examples===
===Examples===
Line 111: Line 111:
  format= ,,,
  format= ,,,
</dpl>
</dpl>
[[Category:Archived pages API16]]

Revision as of 02:56, 13 May 2013

Description

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

[<! removed edit link to red link >]

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

Syntax

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

object The model.

Defined in

libraries/joomla/application/component/controller.php

Importing

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

Source Body

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 edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

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

<CodeExamplesForm />