cakephp 提交至数据库的日期错误

e37o9pze  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(162)

我继承了一个CakePHP 3项目,在特定的日期处理一些读数。我们有一个API连接到一个手机应用程序,它获取井的读数并将它们插入数据库。
读数以及由时间戳创建和更新的读数工作正常,但在这个继承的项目中,它们有一个单独的日期列“reading_date”,显示的是0015-10-05。API控制器的其他方面工作正常,但这仍然给我带来问题。下面是add()方法的代码

/**
   * Add method
   *
   * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
   */
  public function add()
  {
    $wellReading = $this->WellReadings->newEntity();
    if ($this->request->is('post')) {
      $data = $this->request->getData();
      $this->log($this->request->getAttribute('params'));
      $this->ats_log($this->request->getAttribute('params'));
      $data['reading_date'] = date('Y-m-d', strtotime('Today'));
      $data['reading_time'] = date('G:i:s');
      $wellReading = $this->WellReadings->newEntity($wellReading, $data);

      if ($this->WellReadings->save($wellReading)) {
        // we're going to add some related info in here so we can return it with the inserted record in the API
        $operator = $this->WellReadings->Operators->findById($wellReading->operator_id)->first();
        $well = $this->WellReadings->Wells->findById($wellReading->well_id)->first();
        $wellReading->reading_date = date('Y-m-d');
        $wellReading->operator = $operator->name;
        $wellReading->well = $well->identifier;
        $wellReading->region_id = $well->region_id;

        $this->set(['success' => true, 'data' => $wellReading, '_serialize' => ['success', 'data']]);
      } else {
        $this->set(['success' => false, 'errors' => $wellReading->errors(), '_serialize' => ['success', 'errors']]);
      }
    }
  }

我已经从bootstrap.php中删除了UseLocaleParser,因为我读到它会导致旧版本的CakePHP出现问题。
另外,无论我尝试什么,$this-〉log()都没有给予我任何信息。我尝试过使用data变量,并且多次不同的$this-〉request-〉,Params,Attribute,getData迭代,在我的错误日志中什么都没有显示。
ats_log方法应该获取$data并将该变量中的内容写入一个单独的日志文件中。当Cake log函数中什么都没有显示时,我们就有了这个想法,但该文件中的所有内容都是NULL。尽管如此,数据库中仍显示了读数,只有reading_date列是错误的。
任何帮助都是非常感谢的。
编辑:我的首席开发人员已经通知我,我们必须保持LocaleParser。

lf5gs5x2

lf5gs5x21#

所以,这段代码在进入项目后并没有显示出更多的全貌。
我的高级开发人员已经将以前的代码从

$data['reading_date'] = date('d/m/Y', strtotime('Today'));

这导致WellReadingsTable上的验证程序抛出错误。在验证程序中,接受的格式是“mdY”。我不知道为什么有人会允许无效的日期格式进入DB,但在将其切换为“Ymd”后,数据被正确输入。
第二部分,而不是使用$this-〉log();为了捕获数据,我可以使用debug();以使数据显示在文件结构中的单独日志中。

相关问题