Wednesday 28 January 2015

Calling model functions from another classes or views files in Joomla 3+

In Joomla 3+ version, to initialise any model and to call them from another model or helper or view class/files, please follow instruction;

For example, you have a sample model class (as shown below) which exists on
root/components/com_mycomponent/models/gallery.php
or
root/administrator/components/com_mycomponent/models/gallery.php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import the Joomla modellist library
jimport('joomla.application.component.modellist');

/**
 * Gallery Model
 */
class MycomponentModelGallery extends JModelList
{
 /**
  * Method to fetch all list of published galleries
  *
  * @return      string  An SQL query
  */
 protected function getGalleryList()
 {
  ..............
  ..............
  
  return $items;
 }

Now, please use the below format initialising the gallery model and call the functions:
// Use JPATH_ADMINISTRATOR instead of JPATH_SITE to call administrator model classes;
// Include the file path;
JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_mycomponent/models', 'MycomponentModel'); 
// Initialising the included class;
$gallery_model = JModelLegacy::getInstance('Gallery', 'MycomponentModel');
$galleries = $gallery_model->getGalleryList();

No comments:

Post a Comment

Please post any queries and comments here.