HOW TO create different behavior for models?

Discuss everything related to ApPHP Durecty CMF. All versions. Any questions related to the Directy CMF developing or using should be posted to this forum.

Moderators: alexandrleonenko, alexmst

administrator
Site Admin
Site Admin
Posts: 6169
Joined: Jan 7th, '09, 23:18
Contact:

HOW TO create different behavior for models?

Postby administrator » Apr 25th, '17, 20:04

Sometimes you need to create a different behavior for models, where each behavior may be characterized by:
- different JOINT types or no JOINS at all
- different fields returned
- etc.

The simple and elegant way to do this:

1. Define type for relations

Code: Select all

/* @var string (listings|listingsFull|empty)*/
private $_typeRelations = '';



2. Define model() method for model in following way:

Code: Select all

/**
 * Returns the static model of the specified AR class
 * @return void
 */
public static function model($relationType = '')
{
   $model = parent::model(__CLASS__);

   // Set relations type
   if(empty($relationType)){
      $model->_typeRelations = '';
   }else if(in_array($relationType, array('listings', 'listingsFull'))){
      $model->_typeRelations = $relationType;
   }
   
   return $model;
}


3. Use this where it needed:

Code: Select all

/**
 * Defines relations between different tables in database and current $_table
 * @return array
 */
protected function _relations()
{
   $output = array();
   
   if($this->_typeRelations == 'listingsFull'){
      $output = array(
         0 => array(
            self::HAS_MANY,
            $this->_tableListings,
            'id',
            'parent_key' => 'listing_id',
            'joinType'=>self::LEFT_OUTER_JOIN,
            'fields'=>array('is_approved')
         ),
         1 => array(
            self::HAS_MANY,
            $this->_tableListingsTranslation,
            'listing_id',
            'parent_key' => 'listing_id',
            'condition'=>"language_code = '".A::app()->getLanguage()."'",
            'joinType'=>self::LEFT_OUTER_JOIN,
            'fields'=>array('business_name'=>'listing_name')
         ),
      );
   }else if($this->_typeRelations == 'listings'){
      $output = array(
         'listing_id' => array(
            self::HAS_MANY,
            $this->_tableListingsTranslation,
            'listing_id',
            'condition'=>"language_code = '".A::app()->getLanguage()."'",
            'joinType'=>self::LEFT_OUTER_JOIN,
            'fields'=>array('business_name'=>'listing_name')
         )
      );
   }
   
   return $output;
}


or

Code: Select all

/**
 * Used to define custom fields
 * This method should be overridden
 * Usage: 'CONCAT(last_name, " ", first_name)'=>'fullname'
 *        '(SELECT COUNT(*) FROM '.CConfig::get('db.prefix').$this->_tableTranslation.')'=>'records_count'
 */
protected function _customFields()
{
   if($this->_typeRelations == 'listings'){
      return array(
         "IF(
            ".CConfig::get('db.prefix').$this->_tableListings.".finish_publishing != '0000-00-00 00:00:00' AND ".CConfig::get('db.prefix').$this->_tableListings.".finish_publishing != '' AND ".CConfig::get('db.prefix').$this->_tableListings.".finish_publishing < '".LocalTime::currentDateTime()."', 1, 0
         )" => 'status_expired'
      );
   }else{
      return array();
   }
}

Return to “ApPHP Directy CMF {developers/users}”