• <bdo id='y6TYn'></bdo><ul id='y6TYn'></ul>
    <i id='y6TYn'><tr id='y6TYn'><dt id='y6TYn'><q id='y6TYn'><span id='y6TYn'><b id='y6TYn'><form id='y6TYn'><ins id='y6TYn'></ins><ul id='y6TYn'></ul><sub id='y6TYn'></sub></form><legend id='y6TYn'></legend><bdo id='y6TYn'><pre id='y6TYn'><center id='y6TYn'></center></pre></bdo></b><th id='y6TYn'></th></span></q></dt></tr></i><div id='y6TYn'><tfoot id='y6TYn'></tfoot><dl id='y6TYn'><fieldset id='y6TYn'></fieldset></dl></div>

    1. <legend id='y6TYn'><style id='y6TYn'><dir id='y6TYn'><q id='y6TYn'></q></dir></style></legend>

      <tfoot id='y6TYn'></tfoot>

      <small id='y6TYn'></small><noframes id='y6TYn'>

      Laravel 时间戳在没有明确调用的情况下被更新

      Laravel Timestamp Being Updated Without Explicit Call To Do So(Laravel 时间戳在没有明确调用的情况下被更新)
      • <bdo id='4cHgL'></bdo><ul id='4cHgL'></ul>
      • <tfoot id='4cHgL'></tfoot>

          <tbody id='4cHgL'></tbody>
          <i id='4cHgL'><tr id='4cHgL'><dt id='4cHgL'><q id='4cHgL'><span id='4cHgL'><b id='4cHgL'><form id='4cHgL'><ins id='4cHgL'></ins><ul id='4cHgL'></ul><sub id='4cHgL'></sub></form><legend id='4cHgL'></legend><bdo id='4cHgL'><pre id='4cHgL'><center id='4cHgL'></center></pre></bdo></b><th id='4cHgL'></th></span></q></dt></tr></i><div id='4cHgL'><tfoot id='4cHgL'></tfoot><dl id='4cHgL'><fieldset id='4cHgL'></fieldset></dl></div>
          <legend id='4cHgL'><style id='4cHgL'><dir id='4cHgL'><q id='4cHgL'></q></dir></style></legend>
          • <small id='4cHgL'></small><noframes id='4cHgL'>

              • 本文介绍了Laravel 时间戳在没有明确调用的情况下被更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                所以我遇到了 Laravel 更新和保存的烦人问题.我有一个模型/表 Invoiceinvoices,它有一个时间戳 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 时间戳在没有明确调用的情况下被更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                <i id='ohxEr'><tr id='ohxEr'><dt id='ohxEr'><q id='ohxEr'><span id='ohxEr'><b id='ohxEr'><form id='ohxEr'><ins id='ohxEr'></ins><ul id='ohxEr'></ul><sub id='ohxEr'></sub></form><legend id='ohxEr'></legend><bdo id='ohxEr'><pre id='ohxEr'><center id='ohxEr'></center></pre></bdo></b><th id='ohxEr'></th></span></q></dt></tr></i><div id='ohxEr'><tfoot id='ohxEr'></tfoot><dl id='ohxEr'><fieldset id='ohxEr'></fieldset></dl></div>

                  • <small id='ohxEr'></small><noframes id='ohxEr'>

                    <tfoot id='ohxEr'></tfoot>
                          <tbody id='ohxEr'></tbody>

                          <bdo id='ohxEr'></bdo><ul id='ohxEr'></ul>
                          <legend id='ohxEr'><style id='ohxEr'><dir id='ohxEr'><q id='ohxEr'></q></dir></style></legend>