In yii I was looking for a function by which i can insert some fields like created date, modified data, user id , guid or many more attributes . So i have created a master model by which we can insert defined column and no need to insert in each model , For this we just need to create database table structure accordingly . For example i have created the function to auto insert created date, modified date user id and guid
Mastermodel.php

 public function beforeSave()
 {
 $current_time = date('Y-m-d H:i:s');
 if ( $this->isNewRecord )
 {
 if($this->hasAttribute('created'))
 $this->created = $current_time;

if($this->hasAttribute('modified'))
 $this->modified = $current_time;

if($this->hasAttribute('user_id')) // make sure user field name i user_id
 $this->user_id = Yii::app()->user->id;
 }
 if ( ! $this->isNewRecord )
 {
 if($this->hasAttribute('modified'))
 $this->modified = $current_time;
 /* remove this if want updated by id */
 //$this->updated_by = Yii::app()->user->id;
 }
 return parent::beforeSave();
 }
 

if you also want to add guid into your table then create a column in your table with name guid and datatype var char . You need to download this Behaviorfile. Place it in your protected/behaviors directory and add function into Mastermodel.php

 public function behaviors()
 {
 return array(
 'HGuidBehavior' => array(
 'class' => 'application.behaviors.HGuidBehavior',
 ),
 );
 }
 

similarly if you want to create a unique slug from any field . You can add slug behavior into mastermodel.php file .You can download behavior file from here . and add following code in function behaviors

 'SlugBehavior' => array(
 'class' => 'application.behaviors.SlugBehavior',
 'slug_col' => 'slug', // column name to save slug
 'title_col' => 'title', // col field to generate slug
 'max_slug_chars' => 125, // max char
 'overwrite' => false
 ),
 

So your final Mastermodel class is

 abstract class MasterModel extends CActiveRecord
 {
 public function behaviors()
 {
 return array(
 'HGuidBehavior' => array(
 'class' => 'application.behaviors.HGuidBehavior',
 ),
 'SlugBehavior' => array(
 'class' => 'application.behaviors.SlugBehavior',
 'slug_col' => 'slug',
 'title_col' => 'title',
 'max_slug_chars' => 125,
 'overwrite' => false
 ),

);
 }
 public function beforeSave()
 {
 $current_time = date('Y-m-d H:i:s');
 if ( $this->isNewRecord )
 {
 $this->created = $current_time;
 $this->modified = $current_time;
 // if(array_key_exists('user_id',$this))
 $this->user_id = Yii::app()->user->id;
 }
 if ( ! $this->isNewRecord )
 {
 $this->modified = $current_time;
 //$this->updated_by = Yii::app()->user->id;
 }
 return parent::beforeSave();
 }
 }
 

Now you can extend this class to your model like

 class Mymodel extends MasterModel
 {

}
 
http://codingbin.com/wp-content/uploads/2015/11/yii1.jpghttp://codingbin.com/wp-content/uploads/2015/11/yii1-150x150.jpgdevidframeworksYiiyii,yii-modelIn yii I was looking for a function by which i can insert some fields like created date, modified data, user id , guid or many more attributes . So i have created a master model by which we can insert defined column and no need to insert...coding, php, wordpress, yii, cakephp, and  jquery

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request