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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
4c20b57b
Commit
4c20b57b
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\TimestampBehavior updated
parent
8d9f5b48
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
17 deletions
+26
-17
TimestampBehavior.php
framework/behaviors/TimestampBehavior.php
+26
-17
No files found.
framework/behaviors/TimestampBehavior.php
View file @
4c20b57b
...
...
@@ -13,7 +13,7 @@ use yii\db\Expression;
/**
* TimestampBehavior automatically fills the specified attributes with the current timestamp.
*
* To use TimestampBehavior,
simply
insert the following code to your ActiveRecord class:
* To use TimestampBehavior, insert the following code to your ActiveRecord class:
*
* ```php
* use yii\behaviors\TimestampBehavior;
...
...
@@ -31,7 +31,7 @@ use yii\db\Expression;
* with the timestamp when the AR object is being updated. The timestamp value is obtained by `time()`.
*
* If your attribute names are different or you want to use a different way of calculating the timestamp,
* you may configure the [[
attributes
]] and [[value]] properties like the following:
* you may configure the [[
createdAtAttribute]], [[updatedAtAttribute
]] and [[value]] properties like the following:
*
* ```php
* use yii\db\Expression;
...
...
@@ -39,12 +39,10 @@ use yii\db\Expression;
* public function behaviors()
* {
* return [
*
'timestamp' =>
[
* [
* 'class' => TimestampBehavior::className(),
* 'attributes' => [
* ActiveRecord::EVENT_BEFORE_INSERT => 'creation_time',
* ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
* ],
* 'createdAtAttribute' => 'create_time',
* 'updatedAtAttribute' => 'update_time',
* 'value' => new Expression('NOW()'),
* ],
* ];
...
...
@@ -64,17 +62,13 @@ use yii\db\Expression;
class
TimestampBehavior
extends
AttributeBehavior
{
/**
* @var array list of attributes that are to be automatically filled with timestamps.
* The array keys are the ActiveRecord events upon which the attributes are to be filled with timestamps,
* 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_at` and `updated_at` attributes upon AR insertion,
* and update the `updated_at` attribute upon AR updating.
* @var string the attribute that will receive timestamp value
*/
public
$attributes
=
[
BaseActiveRecord
::
EVENT_BEFORE_INSERT
=>
[
'created_at'
,
'updated_at'
],
BaseActiveRecord
::
EVENT_BEFORE_UPDATE
=>
'updated_at'
,
];
public
$createdAtAttribute
=
'created_at'
;
/**
* @var string the attribute that will receive timestamp value
*/
public
$updatedAtAttribute
=
'updated_at'
;
/**
* @var callable|Expression The expression that will be used for generating the timestamp.
* This can be either an anonymous function that returns the timestamp value,
...
...
@@ -86,6 +80,21 @@ class TimestampBehavior extends AttributeBehavior
/**
* @inheritdoc
*/
public
function
init
()
{
parent
::
init
();
if
(
empty
(
$this
->
attributes
))
{
$this
->
attributes
=
[
BaseActiveRecord
::
EVENT_BEFORE_INSERT
=>
[
$this
->
createdAtAttribute
,
$this
->
updatedAtAttribute
],
BaseActiveRecord
::
EVENT_BEFORE_UPDATE
=>
$this
->
updatedAtAttribute
,
];
}
}
/**
* @inheritdoc
*/
protected
function
getValue
(
$event
)
{
if
(
$this
->
value
instanceof
Expression
)
{
...
...
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