• Page:
  • 1

Good Practice to write Model function

ONLINE

Good Practice to write Model function

5 months, 2 weeks ago
Hi All,

Here are few things we should always keep in mind whenever we are writing any model function() in Joomla/uKnowva or in any other framework.

Step 1 - For Save type of model function() never read value from request always pass the values as parameters. So that any one can call the same function from any where via creating object of the same class.

Step 2 - If you are triggering plugins always add a parameter in your function(), name $trigger_plugins = true. If somebody doesn't want to trigger plugin he/she can simply send its value as false.

E.g - 
function save($data,$trigger_plugins =  true){
     //Here $data is what we going to bind and it should already contains the value from $data['created_by'] & $data['created_on']
     if($trigger_plugins){
          JPluginHelper::importPlugin("workflows");
          $dispatcher = JDispatcher::getInstance();
	  $result = $dispatcher->trigger("onBeforeBodSave", array($row, $isnew));
     }
}


Step 3 - For Fetching type of model function() always write another function() for creating the query. So that any one else can directly get the query anywhere via object of same class.

Step 4 - Same as step 2 put a parameter for triggering plugin or not. One more parameter should be there for limit true or not. If limit is false than return all records else according to the limit.

Step 5 - Try to pass array for filters as parameter instead of taking direct values from $this->state->get('filter.name');
E.g - 
function getList($limit=true,$trigger_plugins =  true){
     $query = $this->_getListQuery();
    if($trigger_plugins){
          JPluginHelper::importPlugin("workflows");
          $dispatcher = JDispatcher::getInstance();
	  $result = $dispatcher->trigger("onBeforeGetData", array($row, $isnew));
     }
     $db = JFactory::getDbo();
     if($limit){
         $this->_start =  $this->getStart();
         $this->_total = $this->_getListCount($query);
	 $db->setQuery($query,$this->_start,$this->_limit);
     }else{
        $db->setQuery($query);  
     }  
     return $db->loadObjectList(); 
}
function _getListQuery($filters = array()){
    $query = "SELECT * #__xyz WHERE published=1";
    if(!empty($filters['name'])){
         $query .= " AND name LIKE '%".$filters['name']."%'";
    }
    return $query;
}


Step 6 - If its a user related model than get the user's id as follows -
E.g -
function getLastFilledDataByUser($user_id){
                $user_id = $user_id?$user_id:JFactory::getUser()->id;
		$db = JFactory::getDbo();
		$query = $this->_getLastFilledDataByUser($user_id);
		return $db->setQuery($query)->loadObject();
	}
function _getLastFilledDataByUser($user_id){
      $db = JFactory::getDbo();
      $query = "SELECT * FROM #__workflows_bod WHERE published='1' AND user_id=".$db->quote($user_id);
      return $query;
}


I guess if we are following this protocols while writing model function() we do not need to write the models again in case of API needed. We will be directly calling these model functions() via our API plugin.

@vicky sir please correct me if there is any mistake and add your views which will make the code more clean and reusable.

Thanks
The following user(s) said Thank You: Neemit Shastri, Khurram Shaikh, Abhinav Jaiswal, Nilesh
ONLINE

Re: Good Practice to write Model function

5 months, 2 weeks ago
Yes....this is good, put this in the MVC creator too
The following user(s) said Thank You: Rajat Pal, Sagar S Vemul
  • Page:
  • 1

Upcoming Events

Who's Online