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
e69afe32
Commit
e69afe32
authored
May 17, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactored AutoTimestamp.
parent
14228f86
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
26 deletions
+27
-26
AutoTimestamp.php
yii/behaviors/AutoTimestamp.php
+27
-26
No files found.
yii/behaviors/AutoTimestamp.php
View file @
e69afe32
...
@@ -28,8 +28,9 @@ use yii\db\ActiveRecord;
...
@@ -28,8 +28,9 @@ use yii\db\ActiveRecord;
* }
* }
* ~~~
* ~~~
*
*
* By default, the attribute for keeping the creation time is named as "create_time", and the attribute
* By default, AutoTimestamp will fill the `create_time` attribute with the current timestamp
* for updating time is "update_time". You may customize the names via [[createAttribute]] and [[updateAttribute]].
* when the associated AR object is being inserted; it will fill the `update_time` attribute
* with the timestamp when the AR object is being updated.
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
* @since 2.0
...
@@ -37,15 +38,17 @@ use yii\db\ActiveRecord;
...
@@ -37,15 +38,17 @@ use yii\db\ActiveRecord;
class
AutoTimestamp
extends
Behavior
class
AutoTimestamp
extends
Behavior
{
{
/**
/**
* @var string The name of the attribute to store the creation time. Set to null to not
* @var array list of attributes that are to be automatically filled with timestamps.
* use a timestamp for the creation attribute. Defaults to 'create_time'
* 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 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 the `create_time` attribute upon AR insertion,
* and update the `update_time` attribute upon AR updating.
*/
*/
public
$createAttribute
=
'create_time'
;
public
$attributes
=
array
(
/**
ActiveRecord
::
EVENT_BEFORE_INSERT
=>
'create_time'
,
* @var string The name of the attribute to store the modification time. Set to null to not
ActiveRecord
::
EVENT_BEFORE_UPDATE
=>
'update_time'
,
* use a timestamp for the update attribute. Defaults to 'update_time'
);
*/
public
$updateAttribute
=
'update_time'
;
/**
/**
* @var \Closure|Expression The expression that will be used for generating the timestamp.
* @var \Closure|Expression The expression that will be used for generating the timestamp.
* This can be either an anonymous function that returns the timestamp value,
* This can be either an anonymous function that returns the timestamp value,
...
@@ -61,29 +64,27 @@ class AutoTimestamp extends Behavior
...
@@ -61,29 +64,27 @@ class AutoTimestamp extends Behavior
*/
*/
public
function
events
()
public
function
events
()
{
{
return
array
(
$events
=
array
();
ActiveRecord
::
EVENT_BEFORE_INSERT
=>
'beforeInsert'
,
$behavior
=
$this
;
ActiveRecord
::
EVENT_BEFORE_UPDATE
=>
'beforeUpdate'
,
foreach
(
$this
->
attributes
as
$event
=>
$attributes
)
{
);
if
(
!
is_array
(
$attributes
))
{
$attributes
=
array
(
$attributes
);
}
}
$events
[
$event
]
=
function
()
use
(
$behavior
,
$attributes
)
{
/**
$behavior
->
updateTimestamp
(
$attributes
);
* This is the event handler for the "beforeInsert" event of the associated AR object.
};
*/
public
function
beforeInsert
()
{
if
(
$this
->
createAttribute
!==
null
)
{
$this
->
owner
->
{
$this
->
createAttribute
}
=
$this
->
evaluateTimestamp
(
$this
->
createAttribute
);
}
}
return
$events
;
}
}
/**
/**
* This is the event handler for the "beforeUpdate" event of the associated AR object.
* Updates the attributes with the current timestamp.
* @param array $attributes list of attributes to be updated.
*/
*/
public
function
beforeUpdate
(
)
public
function
updateTimestamp
(
$attributes
)
{
{
if
(
$this
->
updateAttribute
!==
null
)
{
foreach
(
$attributes
as
$attribute
)
{
$this
->
owner
->
{
$this
->
updateAttribute
}
=
$this
->
evaluateTimestamp
(
$this
->
updateA
ttribute
);
$this
->
owner
->
$attribute
=
$this
->
evaluateTimestamp
(
$a
ttribute
);
}
}
}
}
...
...
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