Welcome Guest, Not a member yet? Register   Sign In
Help with My_Model and extending it?
#8

A number of other models already implement similar functionality. For instance, Bonfire's BF_Model does a few things to implement this:
  • Add a series of properties to control the name of these fields, when included
  • Add a series of properties to control whether these fields are included
  • Add a property to control the format of the date fields
So, the properties look something like this:

PHP Code:
   protected $created_field 'created_on';
 
   protected $modified_field 'modified_on';
 
   protected $deleted_field 'deleted';

 
   protected $set_created false;
 
   protected $set_modified false;
    protected $soft_deletes false;
 
   protected $log_user false;

 
   protected $created_by_field 'created_by';
 
   protected $modified_by_field 'modified_by';
 
   protected $deleted_by_field 'deleted_by';

 
   protected $date_format 'datetime'

Note that, in this case, I've defaulted the set_created, set_modified, soft_deletes, and log_user properties to false, which would be ideal if you were adding this to a base model which is already being used by your models, since it would leave the existing models unchanged until you changed the values of those properties in each model. If you wanted one or more of those features supported by default, you would just set the property to true in the base model.

The base model's constructor then checks the properties and sets the relevant observers:

PHP Code:
       if ($this->set_created === true) {
 
           array_unshift($this->before_insert'created_on');
 
       }
 
       if ($this->set_modified === true) {
 
           array_unshift($this->before_update'modified_on');
 
       

(Note: Bonfire only supports created_by and modified_by if created_on and modified_on are also set, and the feature is controlled by the $log_user property. Supporting them independently would require additional variables and observers.)

Then the created_on observer looks something like this (modified_on is very similar):

PHP Code:
   public function created_on($row)
 
   {
 
       if (! array_key_exists($this->created_field$row)) {
 
           $row[$this->created_field] = $this->set_date();
 
       }

 
       return $row;
 
   

Notice that the field name in the row is retrieved from $this->created_field and the date is set by calling $this->set_date(), which looks something like this:


PHP Code:
   protected function set_date($user_date null)
 
   {
 
       $curr_date = empty($user_date) ? time() : $user_date;
 
       $dateFormat '';

 
       switch ($this->date_format) {
 
           case 'datetime':
 
               $dateFormat 'Y-m-d H:i:s';
 
               break;
 
           case 'date':
 
               $dateFormat 'Y-m-d';
 
               break;
 
           case 'int':
 
               // no break;
 
           default:
 
               $dateFormat 'U';
 
               break;
 
       }

 
       return (strtolower($this->config->item('time_reference')) == 'gmt' ?
 
               gmdate($dateFormat$curr_date) :
 
               date($dateFormat$curr_date));
 
   

This allows the model to control the format of the date fields (in $this->date_format), though it would have to be modified if you needed to support multiple formats within a single model. (A method is supplied to change the format at runtime, though.)

Some of the code in BF_Model predates the inclusion of the observers, so some of the features are not implemented as cleanly as possible, but it does give an idea of how to abstract certain elements of the code (such as field names, date types, and whether the features are enabled). These tend to be the types of things that are easily overlooked until you've worked through the implementation a few times.
Reply


Messages In This Thread
RE: Help with My_Model and extending it? - by mwhitney - 04-23-2015, 01:47 PM



Theme © iAndrew 2016 - Forum software by © MyBB