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
db35b63d
Commit
db35b63d
authored
May 16, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes issue #235: Implemented AutoTimestamp.
parent
b3be3710
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
105 additions
and
0 deletions
+105
-0
AutoTimestamp.php
yii/behaviors/AutoTimestamp.php
+105
-0
No files found.
yii/behaviors/AutoTimestamp.php
0 → 100644
View file @
db35b63d
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\behaviors
;
use
yii\base\Behavior
;
use
yii\db\Expression
;
use
yii\db\ActiveRecord
;
/**
* AutoTimestamp will automatically fill the attributes about creation time and updating time.
*
* AutoTimestamp fills the attributes when the associated AR model is being inserted or updated.
* You may specify an AR to use this behavior like the following:
*
* ~~~
* public function behaviors()
* {
* return array(
* 'timestamp' => array(
* 'class' => 'yii\behaviors\AutoTimestamp',
* ),
* );
* }
* ~~~
*
* By default, the attribute for keeping the creation time is named as "create_time", and the attribute
* for updating time is "update_time". You may customize the names via [[createAttribute]] and [[updateAttribute]].
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class
AutoTimestamp
extends
Behavior
{
/**
* @var string The name of the attribute to store the creation time. Set to null to not
* use a timestamp for the creation attribute. Defaults to 'create_time'
*/
public
$createAttribute
=
'create_time'
;
/**
* @var string The name of the attribute to store the modification time. Set to null to not
* use a timestamp for the update attribute. Defaults to 'update_time'
*/
public
$updateAttribute
=
'update_time'
;
/**
* @var string|Expression The expression that will be used for generating the timestamp.
* This can be either a string representing a PHP expression (e.g. 'time()'),
* or an [[Expression]] object representing a DB expression (e.g. `new Expression('NOW()')`).
* If not set, it will use the value of `time()` to fill the attributes.
*/
public
$timestamp
;
/**
* Declares event handlers for the [[owner]]'s events.
* @return array events (array keys) and the corresponding event handler methods (array values).
*/
public
function
events
()
{
return
array
(
ActiveRecord
::
EVENT_BEFORE_INSERT
=>
'beforeInsert'
,
ActiveRecord
::
EVENT_BEFORE_UPDATE
=>
'beforeInsert'
,
);
}
/**
* 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
);
}
}
/**
* This is the event handler for the "beforeUpdate" event of the associated AR object.
*/
public
function
beforeUpdate
()
{
if
(
$this
->
updateAttribute
!==
null
)
{
$this
->
owner
->
{
$this
->
updateAttribute
}
=
$this
->
evaluateTimestamp
(
$this
->
updateAttribute
);
}
}
/**
* Gets the appropriate timestamp depending on the column type $attribute is
* @param string $attribute attribute name
* @return mixed the timestamp value
*/
protected
function
evaluateTimestamp
(
$attribute
)
{
if
(
$this
->
timestamp
instanceof
Expression
)
{
return
$this
->
timestamp
;
}
elseif
(
$this
->
timestamp
!==
null
)
{
return
eval
(
'return '
.
$this
->
timestamp
.
';'
);
}
else
{
return
time
();
}
}
}
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