数据库样式 int 11

后台add.html:

<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Lotterytime')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-lotterytime" data-rule="required" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[lotterytime]" type="text" value="{:date('Y-m-d H:i:s')}">
</div>
</div>

edit.html:
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Lotterytime')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-lotterytime" data-rule="required" class="form-control datetimepicker" data-date-format="YYYY-MM-DD HH:mm:ss" data-use-current="true" name="row[lotterytime]" type="text" value="{$row.lotterytime|datetime}">
</div>
</div>

有时会发现这种方法提交会导致数据库中只存了4位的年份值,因为添加编辑提交的是:Y-m-d H:i:s样式,系统只能检测到第一个年份,需要后台controller对应的model转换一下

转换代码:
// 追加属性
protected $append = [
'lotterytime_text'
];

public function getLotterytimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['lotterytime']) ? $data['lotterytime'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}


protected function setLotterytimeAttr($value)
{
return $value && !is_numeric($value) ? strtotime($value) : $value;
}
posted on 2019-03-28 10:33  baraka  阅读(3890)  评论(0编辑  收藏  举报