Joomla MVC – Load a model from anywhere!

Hello there!

It is good to reuse functionality whenever you can. Many think that there is a 1 to 1 relationship in regards to model and view, but this is not the case. In fact, the model can be called from anywhere. This means that you can quickly hook into functionality without duplicating code, which is always a good thing!

Let say for example that we have a component called com_foo and we want to access it’s items model from say a module (could be a plugin as well).

//first we import some help from the joomla framework

//Load com_foo's items model (notice we are linking to com_foo's model directory)
//and declare 'items' as the first argument. Note this is case sensitive and used to construct the file path.
JLoader::import( 'items', JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_foo' . DS . 'models' );

//Now instantiate the model object using Joomla's camel case type naming protocol.
$items_model = JModel::getInstance( 'items', 'FooModel' );

//Now that we have the item's model loaded, we can set its state
$items_model->setState( 'id', $myItemId );

//and use it's methods!

The Core Team
Editorial Staff Members at 'corePHP'
Editorial staff for the Core Technology Blog for 'corePHP' - news, views insights and advice for e-commerce, marketing technology , web design and development.

18 thoughts on “Joomla MVC – Load a model from anywhere!

  1. Joe LeBlanc

    Good tip, much cleaner than doing a require_once!

    Also, you can drop the DS constant in favor of /. PHP automatically changes / to in paths on Windows. We’re changing it to this for 1.6 core, but it also works now in 1.5.

  2. Adam Docherty

    Just a thing I noticed – there is a naming convention involved in calling a model method.

    From the jview::get() method:

    // Model exists, lets build the method name
    $method = ‘get’.ucfirst($property);

    so if you are sharing a model across multiple views you will want to call like this:

    $this->get( ‘method’ );

    where the method name in the model is:

    function getMethod()

  3. MrRoyce

    Thanks for the tips. It looks like a typo in

    $items_model = JModel::getInstance( Items’, ‘FooModel’ );

    An uppercase ‘I’ in items and only one quote.

  4. Pingback: Advancing from Joomla MVC to Nooku HMVC | Development

  5. Pingback: Torkil Johnsen » Advancing from Joomla MVC to Nooku HMVC

  6. Sergejack

    Why not just doing like this?


    // class MyGolfModelMenu defined in a menu.php file
    $model = JModel::getInstance(‘menu’, ‘MyGolfModel’);

  7. max4ever

    thank you!!!
    i used the shorter version Sergejack suggested


    $model = JModel::getInstance(‘menu’, ‘MyGolfModel’);

  8. Adam Docherty

    To import a another model within a view and have $this->get() work properly you can just add this method to your view and replace banners with the model you want to load.

    function get( $method )
    if( !isset( $this->model ) ) {
    JLoader::import( ‘banners’, JPATH_ADMINISTRATOR . DS . ‘components’ . DS . ‘com_banners’ . DS . ‘models’ );

    $this->model = JModel::getInstance( ‘Banners’, ‘BannersModel’ );

    $method = ‘get’ . $method;

    return $this->model->{$method}();

  9. Tim

    It works great. Thanks for the tips.
    Issues with this approach were the form definition file and language profile could not be found. I have to copy those files to my own component location.
    Does anyone have work-around for these?

  10. Andreas

    I’ve tried this with
    JLoader::import(‘article’, JPATH_ADMINISTRATOR . DS . ‘components’ . DS . ‘com_content’ . DS . ‘models’ );
    to ceate a new article in the API.
    However, the article.php model calls
    require_once JPATH_COMPONENT_ADMINISTRATOR.’/helpers/content.php';
    and JPATH_COMPONENT_ADMINISTRATOR points to my own component…
    Any ideas?

  11. Find Out More

    At this time it seems like WordPress is the top blogging
    platform available right now. (from what I’ve read) Is that what you are using on your blog?

Comments are closed.