我有一个通过请求对象更新模型的控制器。它看起来
$save_series = Series::where('id','=',$request->input('data.item.id'))->with('booking')->first();
$save_series->name = $request->input('data.item.name');
$save_series->color = str_replace('#', '', $request->input('data.item.color'));
$save_series->production_company = $request->input('data.item.production_company');
$save_series->booking->start = $request->input('data.item.booking.start');
$save_series->booking->end = $request->input('data.item.booking.end');
$save_series->booking->save();
$save_series->save();
保存后,相关的"booking"对象被更新。如果改变了"结束"日期属性,则将"开始"属性的时间设置为当前时间;不应该是这样的!它似乎完全忽略了解析后的$request开始时间。
在与预订相关的模型上,我放置了一个setter mutator来查看问题是否存在于该点之前-日期被正确解析,但是一旦它到达数据库,它就发生了变化。我的设置(仅针对此测试)如下所示:
public function setStartAttribute($time) {
error_log(":::::::::::TEST BELOW:");
error_log($time);
return $time;
}
错误日志结果为:
PHP message: :::::::::::TEST BELOW:
PHP message: 2016-10-27 00:00:00
但是这个记录的数据库字段"start"是:
2016-10-27 13:07:53
有人能解释一下到底发生了什么吗?我完全迷路了!
—附加信息—
迁移导致问题:
Schema::create('calendar_events', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('calendar_event_type')->references('id')->on('calendar_event_types');
$table->timestampTz('start');
$table->timestampTz('end');
$table->softDeletes();
$table->timestamps();
});
修复问题的迁移解决方案:
Schema::create('calendar_events', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('calendar_event_type')->references('id')->on('calendar_event_types');
$table->dateTime('start');
$table->dateTime('end');
$table->softDeletes();
$table->timestamps();
});
我认为这与MySQL对timestampTz的支持以及我将其发送到数据库的格式有关。改变了这个,我的问题现在解决了,非常感谢@FrankProvost!
我假定现在不可能维护时区。在我的用例中,时区不是问题,但是如果有。
我不知道这样做的原因,但是当添加额外的时间戳(除了默认创建和更新)时,您应该在添加默认时间戳之后添加额外的时间戳。
否则,表似乎将"set current timestamp on update"标志设置在错误的列上。
不是$table->timestamp('my_custom_date');
$table->timestamps();
总是选择
$table->timestamps();
$table->timestamp('my_custom_date');