所以我遇到了 Laravel 更新和保存的烦人问题.我有一个模型/表 Invoice
和 invoices
,它有一个时间戳 sent_at
.
So I'm running into an annoying problem with Laravel update and save. I have a model/table Invoice
and invoices
, that has a timestamp sent_at
.
Invoice.php
class Invoice extends Model {
protected $dates = [
"sent_at",
];
}
我有以下更新Invoice
的函数:
InvoicesController.php
:
public function postPayInvoice(Request $request, $invoiceId){
$user = $this->apiResponse->user;
$invoiceItemIds = $request->input("invoice_item_ids");
$invoice = Invoice::with(["invoiceItems" => function($subQuery) use($invoiceItemIds){
return $subQuery->whereIn("invoice_items.id", $invoiceItemIds);
}])->where("id", "=", $invoiceId)->first();
Log::info("Load: ".$invoice->sent_at);
DB::beginTransaction();
try {
foreach($invoice->invoiceItems AS $invoiceItem){
$invoiceItem->status = "paid";
$invoiceItem->paid_at = Carbon::now();
$invoiceItem->save();
}
$totalInvoices = $invoice->invoiceItems()->count();
$paidInvoiceItems = $invoice->invoiceItems()->where("status", "=", "paid")->count();
if($totalInvoices == $paidInvoiceItems){
$invoice->status = "paid";
$invoice->paid_at = Carbon::now();
} else {
$invoice->status = "partially_paid";
}
Log::info("Pre: ".$invoice->sent_at);
$invoice->save();
Log::info("Post: ".$invoice->sent_at);
} catch(Exception $ex){
DB::rollBack();
return $this->apiResponse->returnFail([], "Unable to Pay Invoice: ".$ex->getMessage(), 200);
}
DB::{$request->input("rollback", null) ? "rollback" : "commit"}();
Log::info("Post Commit: ".$invoice->sent_at);
return $this->apiResponse->returnSuccess($invoice, "Invoice paid!", 200);
}
这样做是支付选定的InvoiceItems
(Invoice
的子模型),并且,如果所有InvoiceItems
都被标记为支付
,然后将invoices.status
更新为paid
(或partially_paid
)并将invoices.paid_at
更新为Carbon::now()
(或null
).
What this does is pays the selected InvoiceItems
(child model of Invoice
), and, if all InvoiceItems
are marked as paid
, then updates invoices.status
to paid
(or partially_paid
) and invoices.paid_at
to Carbon::now()
(or null
).
这一切正常,但不知何故,这段代码也在更新 sent_at
(因此是 Log
语句).当代码加载 Invoice
时,在应用所有保存逻辑后,在保存后,最后在提交后,记录 sent_at
属性:
This all works fine, but somehow, this code is also updating sent_at
(hence the Log
statements). When the code loads the Invoice
, after applying all save logic, right after saving and finally right after committing, the sent_at
attribute is logged:
[2019-05-08 12:43:24] local.INFO: Load: 2019-05-08 12:42:50
[2019-05-08 12:43:24] local.INFO: 前: 2019-05-08 12:42:50
[2019-05-08 12:43:24] local.INFO: Post: 2019-05-08 12:42:50
[2019-05-08 12:43:24] local.INFO: Post Commit: 2019-05-08 12:42:50
[2019-05-08 12:43:24] local.INFO: Load: 2019-05-08 12:42:50
[2019-05-08 12:43:24] local.INFO: Pre: 2019-05-08 12:42:50
[2019-05-08 12:43:24] local.INFO: Post: 2019-05-08 12:42:50
[2019-05-08 12:43:24] local.INFO: Post Commit: 2019-05-08 12:42:50
如您所见,sent_at
时间戳始终为 2019-05-08 12:42:50
.但是我一重新查询数据库,时间戳就是2019-05-08 12:43:24
,也就是paid_at
和的值updated_at
时间戳.
As you can see, the sent_at
timestamp is consistently 2019-05-08 12:42:50
. But as soon as I re-query the database, the timestamp is 2019-05-08 12:43:24
, which is the value of the paid_at
and updated_at
timestamps.
(status
, sent_at
, paid_at
, created_at
, updated_at
)
请注意,这是从 API 调用的,随后请求加载 Invoice
模型列表,该模型具有以下逻辑来确定附加逻辑:
Note this is called from an API, with a subsequent request to load a list of Invoice
models, which has the following logic to determine so additional logic:
$cutoff = $this->sent_at->addDays(3)->endOfDay();
但我看不出这如何修改 sent_at
列(以下不会调用保存/更新,即使调用了,2019-05-08 12:43:24
不等于 addDays(3)->endOfDay();
But I don't see how that could modify the sent_at
column (no save/update is called following, and even if it did, 2019-05-08 12:43:24
does not equate to addDays(3)->endOfDay();
有人见过这个吗?它在另一个视图中搞乱了一些排序逻辑,所以我最终需要修复它......
Has anyone seen this before? It's messing up some ordering logic in another view, so I need to fix it eventually...
编辑
如果我禁用 $invoice->save();
,它的 updated_at
时间戳仍然更新,但我不知道为什么.而且,奇怪的是,禁用 $invoiceTransaction->save();
和 $invoiceItem->save();
不会导致 updated_at代码>...确实会导致错误的数据,但仍在开发中.
If I disable $invoice->save();
, it's updated_at
timestamp is still updated, but I have no idea why. And, oddly enough, disabling $invoiceTransaction->save();
and $invoiceItem->save();
results in no change to updated_at
... Does result in bad data, but this is still in development.
二次编辑
"CREATE TABLE `invoices` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`account_id` int(11) NOT NULL,
`description` text,
`subtotal` decimal(10,2) NOT NULL,
`grand_total` decimal(10,2) NOT NULL,
`status` enum('pending','sent','partially_paid','paid') NOT NULL DEFAULT
'pending',
`sent_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`paid_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8"
我认为那里存在问题:
sent_at
时间戳 NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
sent_at
timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
这是由于 MySQL 5.7 配置,而不是 Laravel.
This is due to the MySQL 5.7 configuration, and not Laravel.
表中的第一个 TIMESTAMP 列,如果未使用 NULL 属性或显式 DEFAULT 或 ON UPDATE 属性显式声明,将自动使用 DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP 属性声明.
The first TIMESTAMP column in a table, if not explicitly declared with the NULL attribute or an explicit DEFAULT or ON UPDATE attribute, is automatically declared with the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP attributes.
src - Mysql 文档
修复方法是在迁移中将时间戳设置为可为空,和/或手动更改表.
The fix is to set the timestamp to nullable in the migration, and/or alter the table manually.
ALTER TABLE invoices CHANGE sent_at sent_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP;
这篇关于Laravel 时间戳在没有明确调用的情况下被更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!