所以我在 Laravel 更新和保存方面遇到了一个烦人的问题。我有一个模型/表 Invoice 和 invoices,它有一个时间戳 sent_at。
发票.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 都标记为 paid,然后将 invoices.status 更新为 paid(或 partially_paid)并将 invoices.paid_at 更新为Carbon::now()(或null)。
这一切工作正常,但不知何故,这段代码也在更新 sent_at(因此 \Log 语句)。当代码加载 Invoice 时,在应用所有保存逻辑之后,在保存之后和提交之后,sent_at 属性被记录:
[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 时间戳。
(status, sent_at, paid_at, created_at, updated_at)
请注意,这是从 API 调用的,随后请求加载 Invoice 模型列表,该模型具有以下逻辑来确定附加逻辑:
$cutoff = $this->sent_at->addDays(3)->endOfDay();
但我看不出如何修改 sent_at 列(以下没有调用保存/更新,即使调用了,2019-05-08 12:43: 24 不等于 addDays(3)->endOfDay();
有没有人见过这个?它在另一个 View 中弄乱了一些排序逻辑,所以我最终需要修复它...
编辑
如果我禁用 $invoice->save();,它的 updated_at 时间戳仍然更新,但我不知道为什么。而且,奇怪的是,禁用 $invoiceTransaction->save(); 和 $invoiceItem->save(); 不会导致 updated_at 发生变化...确实会导致数据错误,但这仍在开发中。
二次编辑
"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_attimestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
最佳答案
这是由于 MySQL 5.7 配置,而不是 Laravel。
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.
解决方法是在迁移中将时间戳设置为可为空,和/或手动更改表。
ALTER TABLE invoices CHANGE sent_at sent_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP;
关于php - Laravel 时间戳在没有明确调用的情况下被更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56045660/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
我最近决定从我的系统中卸载RVM。在thispage提出的一些论点说服我:实际上,我的决定是,我根本不想担心Ruby的多个版本。我只想使用1.9.2-p290版本而不用担心其他任何事情。但是,当我在我的Mac上运行ruby--version时,它告诉我我的版本是1.8.7。我四处寻找如何简单地从我的Mac上卸载这个Ruby,但奇怪的是我没有找到任何东西。似乎唯一想卸载Ruby的人运行linux,而使用Mac的每个人都推荐RVM。如何从我的Mac上卸载Ruby1.8.7?我想升级到1.9.2-p290版本,并且我希望我的系统上只有一个版本。 最佳答案
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle