Back to Top

Joomla – How to use a model in another component

Updated 17 October 2017

Introduction

In this blog we will learn about using a Joomla model outside the scope of a component. As you must be already knowing about JModelLegacy Class of Joomla which provides methods to get Model, but it has some limits as you cannot access all models using this JModelLegacy::getInstance( ).

Solution – Admin

To get all models of any component you have to create your own function, it is recommended to create a static helper function, as you may need to use the function at several places.

/**
 * Webkul Software.
 *
 * @category  Webkul
 * @author    Webkul
 * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 *
 * getModelAdmin function
 *
 * @param [String] $component name of component
 * @param string $name name of model
 * @param string $prefix
 * @return Object
 */
function getModelAdmin($component, $name = 'Custom', $prefix = 'CustomModel')
{
    if (!isset($component)) {
        JFactory::getApplication()->enqueueMessage(JText::_("COM_ERROR_MSG"), 'error');
        return false;
    }
    $path=JPATH_ADMINISTRATOR . '/components/'.$component.'/models/';
    JModelLegacy::addIncludePath($path);
    require_once $path.strtolower($name).'.php';
    $model = JModelLegacy::getInstance($name, $prefix);
    // If the model is not loaded then $model will get false
    if ($model == false) {
        $class=$prefix.$name;
        // initilize the model
        new $class();
        $model = JModelLegacy::getInstance($name, $prefix);
    }
    return $model;
}

For Site

A similar function for accessing site models:

/**
 * Webkul Software.
 *
 * @category  Webkul
 * @author    Webkul
 * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 *
 *
 * getModelSite function
 *
 * @param [String] $component name of component
 * @param string $name name of model
 * @param string $prefix
 * @return Object
 */
function getModelSite($component, $name = 'Custom', $prefix = 'CustomModel')
{
    if (!isset($component)) {
        JFactory::getApplication()->enqueueMessage(JText::_("COM_ERROR_MSG"), 'error');
        return false;
    }
    $path=JPATH_SITE . '/components/'.$component.'/models/';
    JModelLegacy::addIncludePath($path);
    require_once $path.strtolower($name).'.php';
    $model = JModelLegacy::getInstance($name, $prefix);
    // If the model is not loaded then $model will get false
    if ($model == false) {
        $class=$prefix.$name;
        // initilize the model
        new $class();
        $model = JModelLegacy::getInstance($name, $prefix);
    }
    return $model;
}

If you find this blog useful please like and share.

Start your headless eCommerce
now.
Find out More

Current Product Version - 3

Supported Framework Version - 3

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home

Table of Content