duannian7116 2014-12-24 06:50
浏览 32

YII2 Restful post和TimestampBehavior

I'm trying to use TimestampBehavior in my Restful API created Yii2, but when I create a new entity using the POST endpoint, the return data is quite strange.

{
  "i18_id": 3,
  "language": "es",
  "text": "un test de traducción",
  "created_at": {
    "expression": "NOW()",
    "params": [ ]
  },
  "updated_at": {
    "expression": "NOW()",
    "params": [ ]
  },
  "id": 2
}

When I GET the entity later everything looks fine.

Here is how I've declared the Behavior in my model:

public function behaviors()
{
    return [
        'timestamp' => [
            'class' => 'yii\behaviors\TimestampBehavior',
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
            ],
            'value' => new Expression('NOW()'),
        ],
    ];
}
  • 写回答

3条回答 默认 最新

  • dsgdfh302506 2015-04-30 08:57
    关注

    I guess you have added the Entry in your test and return the model just added.

    I guess a simple $model->refresh(); will fill the values.

    See official docs here.

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题