0

当我运行以下代码时,订单确实保存在数据库中。但在某些情况下id,保存的结果是。0在数据库中,Order 的正常 ID 为 100,101。

        $this->order->save(); // new order not in database yet
        //dd($this->order);

        $id = $this->order->id;
        if ($id === 0  || $id === null) {
            throw new CreateOrderFailedException('Order ID = null');
        }

上面的代码在 Homestead 沙盒上本地运行。当我将代码推送到暂存和生产环境时(它们都是 Vapor 环境)。保存函数总是返回一个$order->id0 不知所措的原因。再次在本地工作。在会破坏这种情况的蒸汽环境中可能有什么不同?相同的 php 版本,相同的 laravel 版本。相同的(会话)缓存,相同的数据库。

这些dd语句在本地显示一个全新的 ID,但0在蒸汽(登台/生产)环境中。任何想法都会非常受欢迎。

我试过了:

  • $id = DB::table('orders')->insertGetId([atts])
  • $this->order = new Order([atts]); $this->order->save()
  • $this->order->create([atts])

结果保持不变(在本地完美运行,而不是在 Vapor 上部署一次)

编辑,做了一个非常肮脏的修复,现在解决了这个问题

        $id = DB::select('SELECT id FROM orders ORDER BY id DESC LIMIT 1');

        if ($id === null || $id[0]->id === 0) {
            throw new CreateOrderFailedException('Order ID = null');
        }

        $this->order->id = $id[0]->id;
        $this->addProductsToOrder($id[0]->id);
        $this->updateStatusAndCreateNote($id[0]->id);

因此,在没有 Model 类的情况下工作似乎可以正确检索最新的 ID。但是,如果有人可以详细说明这个 Eloquent 返回 id = 0 的问题,我会全力以赴。

根据要求dd放入保存功能的属性。

当地的

array:19 [
  "order_nr" => "OR2112-0000000109"
  "is_weekly_order" => 1
  "type" => 0
  "user_id" => 540
  "admin_id" => null
  "sales_id" => null
  "shipping_costs" => 0
  "status" => 0
  "reference" => null
  "shipping_method" => 3
  "signed_shipping" => false
  "country_id" => "NL"
  "is_demo" => false
  "comment" => null
  "shipping_address_id" => 679
  "billing_address_id" => 679
  "is_master_key_system" => null
  "created_at" => Carbon\Carbon @1640370798 {#1673
    #endOfTime: false
    #startOfTime: false
    #constructedObjectId: "000000007fdf7f5c0000000072fb8804"
    #localMonthsOverflow: null
    #localYearsOverflow: null
    #localStrictModeEnabled: null
    #localHumanDiffOptions: null
    #localToStringFormat: null
    #localSerializer: null
    #localMacros: null
    #localGenericMacros: null
    #localFormatFunction: null
    #localTranslator: null
    #dumpProperties: array:3 [
      0 => "date"
      1 => "timezone_type"
      2 => "timezone"
    ]
    #dumpLocale: null
    #dumpDateProperties: null
    date: 2021-12-24 19:33:18.488342 Europe/Amsterdam (+01:00)
  }
  "updated_at" => Carbon\Carbon @1640370798 {#1640
    #endOfTime: false
    #startOfTime: false
    #constructedObjectId: "000000007fdf7fbd0000000072fb8804"
    #localMonthsOverflow: null
    #localYearsOverflow: null
    #localStrictModeEnabled: null
    #localHumanDiffOptions: null
    #localToStringFormat: null
    #localSerializer: null
    #localMacros: null
    #localGenericMacros: null
    #localFormatFunction: null
    #localTranslator: null
    #dumpProperties: array:3 [
      0 => "date"
      1 => "timezone_type"
      2 => "timezone"
    ]
    #dumpLocale: null
    #dumpDateProperties: null
    date: 2021-12-24 19:33:18.488374 Europe/Amsterdam (+01:00)
  }
]

汽:

array:19 [
  "order_nr" => "OR2112-0000000079"
  "is_weekly_order" => 1
  "type" => 0
  "user_id" => 570
  "admin_id" => null
  "sales_id" => null
  "shipping_costs" => 0
  "status" => 0
  "reference" => null
  "shipping_method" => 3
  "signed_shipping" => false
  "country_id" => "NL"
  "is_demo" => false
  "comment" => null
  "shipping_address_id" => 550
  "billing_address_id" => 551
  "is_master_key_system" => null
  "created_at" => Carbon\Carbon @1640371111 {#2036
    #endOfTime: false
    #startOfTime: false
    #constructedObjectId: "0000000003fd91d000000000742f9e90"
    #localMonthsOverflow: null
    #localYearsOverflow: null
    #localStrictModeEnabled: null
    #localHumanDiffOptions: null
    #localToStringFormat: null
    #localSerializer: null
    #localMacros: null
    #localGenericMacros: null
    #localFormatFunction: null
    #localTranslator: null
    #dumpProperties: array:3 [
      0 => "date"
      1 => "timezone_type"
      2 => "timezone"
    ]
    #dumpLocale: null
    #dumpDateProperties: null
    date: 2021-12-24 19:38:31.570906 Europe/Amsterdam (+01:00)
  }
  "updated_at" => Carbon\Carbon @1640371111 {#2025
    #endOfTime: false
    #startOfTime: false
    #constructedObjectId: "0000000003fd91cd00000000742f9e90"
    #localMonthsOverflow: null
    #localYearsOverflow: null
    #localStrictModeEnabled: null
    #localHumanDiffOptions: null
    #localToStringFormat: null
    #localSerializer: null
    #localMacros: null
    #localGenericMacros: null
    #localFormatFunction: null
    #localTranslator: null
    #dumpProperties: array:3 [
      0 => "date"
      1 => "timezone_type"
      2 => "timezone"
    ]
    #dumpLocale: null
    #dumpDateProperties: null
    date: 2021-12-24 19:38:31.571011 Europe/Amsterdam (+01:00)
  }
]

即使在这里,我也没有注意到任何实质性的东西,因为我已经在想了。我唯一的其他可能的解释是,蒸汽与数据库相关的一些事情是异步的。除此之外,我没有线索。

4

0 回答 0