2017 © Pedro Peláez
 

yii2-extension yii2-creator-behavior

Creator behavior for Yii2

image

tugmaks/yii2-creator-behavior

Creator behavior for Yii2

  • Monday, May 30, 2016
  • by tugmaks
  • Repository
  • 0 Watchers
  • 0 Stars
  • 210 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Yii2 creator behavior

Creator behavior for Yii2, (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require --prefer-dist tugmaks/yii2-creator-behavior "*"

or add, (*4)

"tugmaks/yii2-creator-behavior": "*"

to the require section of your composer.json file., (*5)

Usage

CreatorBehavior automatically fills the specified attributes with the current user id., (*6)

To use CreatorBehavior, insert the following code to your ActiveRecord class:, (*7)

  use tugmaks\behaviors\CreatorBehavior;

  public function behaviors()
  {
      return [
          CreatorBehavior::className(),
      ];
  }

By default, CreatorBehavior will fill the created_by and updated_by attributes with the current user id when the associated AR object is being inserted; it will fill the updated_by attribute with the current user id when the AR object is being updated. The user id value is obtained by Yii::$app->user->id., (*8)

For the above implementation to work with MySQL database, please declare the columns(created_by, updated_by) as int(11)., (*9)

If your attribute names are different or you want to use a different way of retrieving user id, you may configure the [[createdByAttribute]], [[updatedByAttribute]] and [[value]] properties like the following:, (*10)

  public function behaviors()
  {
      return [
          [
              'class' => CreatorBehavior::className(),
              'createdByAttribute' => 'creator_id',
              'updatedByAttribute' => 'updater_id',
              'value'=>function($event){
                   return \Yii::$app->user->identity->getCustomId();
               }
          ],
      ];
  }

The Versions

30/05 2016

dev-master

9999999-dev

Creator behavior for Yii2

  Sources   Download

MIT

The Requires

 

by Avatar tugmaks

extension yii2 behavior