Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
8d9f5b48
Commit
8d9f5b48
authored
Jun 19, 2014
by
Alexander Kochetov
Committed by
Qiang Xue
Jul 23, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
\yii\behaviors\BlameableBehavior updated
parent
471d0536
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
16 deletions
+26
-16
BlameableBehavior.php
framework/behaviors/BlameableBehavior.php
+26
-16
No files found.
framework/behaviors/BlameableBehavior.php
View file @
8d9f5b48
...
@@ -14,7 +14,7 @@ use yii\db\BaseActiveRecord;
...
@@ -14,7 +14,7 @@ use yii\db\BaseActiveRecord;
/**
/**
* BlameableBehavior automatically fills the specified attributes with the current user ID.
* BlameableBehavior automatically fills the specified attributes with the current user ID.
*
*
* To use BlameableBehavior,
simply
insert the following code to your ActiveRecord class:
* To use BlameableBehavior, insert the following code to your ActiveRecord class:
*
*
* ```php
* ```php
* use yii\behaviors\BlameableBehavior;
* use yii\behaviors\BlameableBehavior;
...
@@ -30,7 +30,7 @@ use yii\db\BaseActiveRecord;
...
@@ -30,7 +30,7 @@ use yii\db\BaseActiveRecord;
* By default, BlameableBehavior will fill the `created_by` and `updated_by` attributes with the current user ID
* By default, BlameableBehavior 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
* 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. If your attribute names are different, you may configure
* with the current user ID when the AR object is being updated. If your attribute names are different, you may configure
* the [[
attributes]] property
like the following:
* the [[
createdByAttribute]] and [[updatedByAttribute]] properties
like the following:
*
*
* ```php
* ```php
* public function behaviors()
* public function behaviors()
...
@@ -38,10 +38,8 @@ use yii\db\BaseActiveRecord;
...
@@ -38,10 +38,8 @@ use yii\db\BaseActiveRecord;
* return [
* return [
* [
* [
* 'class' => BlameableBehavior::className(),
* 'class' => BlameableBehavior::className(),
* 'attributes' => [
* 'createdByAttribute' => 'author_id',
* ActiveRecord::EVENT_BEFORE_INSERT => 'author_id',
* 'updatedByAttribute' => 'updater_id',
* ActiveRecord::EVENT_BEFORE_UPDATE => 'updater_id',
* ],
* ],
* ],
* ];
* ];
* }
* }
...
@@ -49,22 +47,19 @@ use yii\db\BaseActiveRecord;
...
@@ -49,22 +47,19 @@ use yii\db\BaseActiveRecord;
*
*
* @author Luciano Baraglia <luciano.baraglia@gmail.com>
* @author Luciano Baraglia <luciano.baraglia@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Alexander Kochetov <creocoder@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
class
BlameableBehavior
extends
AttributeBehavior
class
BlameableBehavior
extends
AttributeBehavior
{
{
/**
/**
* @var array list of attributes that are to be automatically filled with the current user ID.
* @var string the attribute that will receive current user ID value
* The array keys are the ActiveRecord events upon which the attributes are to be filled with the user ID,
* and the array values are the corresponding attribute(s) to be updated. You can use a string to represent
* a single attribute, or an array to represent a list of attributes.
* The default setting is to update both of the `created_by` and `updated_by` attributes upon AR insertion,
* and update the `updated_by` attribute upon AR updating.
*/
*/
public
$attributes
=
[
public
$createdByAttribute
=
'created_by'
;
BaseActiveRecord
::
EVENT_BEFORE_INSERT
=>
[
'created_by'
,
'updated_by'
],
/**
BaseActiveRecord
::
EVENT_BEFORE_UPDATE
=>
'updated_by'
,
* @var string the attribute that will receive current user ID value
];
*/
public
$updatedByAttribute
=
'updated_by'
;
/**
/**
* @var callable the value that will be assigned to the attributes. This should be a valid
* @var callable the value that will be assigned to the attributes. This should be a valid
* PHP callable whose return value will be assigned to the current attribute(s).
* PHP callable whose return value will be assigned to the current attribute(s).
...
@@ -81,6 +76,21 @@ class BlameableBehavior extends AttributeBehavior
...
@@ -81,6 +76,21 @@ class BlameableBehavior extends AttributeBehavior
public
$value
;
public
$value
;
/**
/**
* @inheritdoc
*/
public
function
init
()
{
parent
::
init
();
if
(
empty
(
$this
->
attributes
))
{
$this
->
attributes
=
[
BaseActiveRecord
::
EVENT_BEFORE_INSERT
=>
[
$this
->
createdByAttribute
,
$this
->
updatedByAttribute
],
BaseActiveRecord
::
EVENT_BEFORE_UPDATE
=>
$this
->
updatedByAttribute
,
];
}
}
/**
* Evaluates the value of the user.
* Evaluates the value of the user.
* The return result of this method will be assigned to the current attribute(s).
* The return result of this method will be assigned to the current attribute(s).
* @param Event $event
* @param Event $event
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment