Created
March 26, 2015 10:01
-
-
Save you-think-you-are-special/3159614ae9e88324bb7e to your computer and use it in GitHub Desktop.
Yii2 UserSetter Behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by Alexander Litvinov | |
* Email: alexander@codeordie.ru | |
* May be freely distributed under the MIT license | |
*/ | |
namespace common\behaviors; | |
use Yii; | |
use yii\base\Behavior; | |
use yii\db\ActiveRecord; | |
/** | |
* Class UserSetter | |
*/ | |
class UserSetter extends Behavior | |
{ | |
/** | |
* @var string | |
*/ | |
public $userIdName = 'user_id'; | |
/** | |
* @return array | |
*/ | |
public function events() | |
{ | |
return [ | |
ActiveRecord::EVENT_BEFORE_INSERT => 'userSetter', | |
ActiveRecord::EVENT_BEFORE_UPDATE => 'userSetter' | |
]; | |
} | |
public function userSetter() | |
{ | |
$UserIdName = $this->userIdName; | |
$this->owner->$UserIdName = Yii::$app->user->id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment