Yii时间戳是如何工作的?

Yii时间戳是如何工作的?

问题描述:

Yii says it can automatically set the timestamps 'created_at': http://www.yiiframework.com/doc-2.0/guide-concept-behaviors.html#using-timestampbehavior

However, every time I created a record, the time is blank:

$ yii kw/test
1437683634

mysql> select * from ad_group_keyword_network where ad_group_keyword_id = 1;
+---------------------+-
| created_at          | 
+---------------------+-
| 0000-00-00 00:00:00 | 
| 0000-00-00 00:00:00 | 
+---------------------+-

  public function actionTest() {
    $agkn = new AdGroupKeywordNetwork();
    $agkn->save();
    echo $agkn->created_at;

I tried with Schema::TYPE_TIMESTAMP and I changed the field and also tried with Schema::TYPE_DATETIME. They both return an integer timestamp in the field after save, but the time is all 0s in the database.

public function behaviors()
{
    return [
        [
            'class' => TimestampBehavior::className(),
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ActiveRecord::EVENT_BEFORE_UPDATE => [],

Yii表示它可以自动设置时间戳'created_at': http://www.yiiframework.com/doc-2.0/guide-concept-behaviors.html#using-timestampbehavior p>

但是,每次创建记录时,时间都是空白的: p>

  $ yii kw / test 
1437683634  
 
mysql> 从ad_group_keyword_network中选择*,其中ad_group_keyword_id = 1; 
 + --------------------- +  -  
 |  created_at |  
 + --------------------- +  -  \ N |  
00:00:00 | |
00:00:00 | + --------------------- + - 公共函数actionTest(){ $ agkn = new AdGroupKeywordNetwork(); $ agkn- > save(); echo $ agkn-> created_at; code> pre>

我尝试使用 Schema :: TYPE_TIMESTAMP code>并更改了 该字段也尝试使用 Schema :: TYPE_DATETIME code>。 它们都在保存后返回字段中的整数时间戳,但数据库中的时间全是0。 p>

 公共函数行为()
 {
 
返回[
  [
'class'=>  TimestampBehavior :: className(),
'attributes'=>  [
 ActiveRecord :: EVENT_BEFORE_INSERT =>  ['created_at'],
 ActiveRecord :: EVENT_BEFORE_UPDATE =>  [],
  code>  pre> 
  div>

your created_at must be type INT (11) for the Yii 2 timestamp behaviour work. I think you are using type TIMESTAMP which will not work.

Based on what Marc and Michael said, I used this and it worked.

public function behaviors()
{
    return [
        [
            'class' => TimestampBehavior::className(),
            'createdAtAttribute' => 'created_at',
            'updatedAtAttribute' => null,
            'value' => function(){return date(DATE_ATOM);},

$ yii kw/test
2015-07-23T16:59:17-04:00
...
+---------------------+
| created_at          |
+---------------------+
| 2015-07-23 16:59:17 |

$value is the expression that will be used for generating the timestamp, by default it will use the value of time() unless you assign to it an anonymous function that returns the timestamp value :

'timestamp' => [
    'class' => TimestampBehavior::className(),
    'attributes' => [
        ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
        ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
    ],
    'value' => function() { return date('U'); },
],

Or an yii\db\Expression object representing a DB expression :

'timestamp' => [
    'class' => TimestampBehavior::className(),
    'attributes' => [
        ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
        ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
    ],
    'value' => new \yii\db\Expression('NOW()'), // MySql exp
],