Cake PHP PatchEntities 保存 epoc

Cake PHP PatchEntities saveing the epoc

我继承了一个蛋糕应用程序,但我在处理日期时间方面遇到了一些问题。

当我保存提交时,我必须将日期格式设置为 Y-m-d。这允许 patchEntities() 保存,但是当我检查数据库时,日期是 epoc。

当前代码

$get_data = $this->request->getData();
                foreach ($get_data as $key => $value) {
                    if(isset($get_data[$key]['lease_submissions']) && is_array($get_data[$key]['lease_submissions'])){
                        foreach($get_data[$key]['lease_submissions'] as $key_i => $value_i){
                            var_dump(date("Y-m-d", strtotime($get_data[$key]['lease_submissions'][$key_i]['reporting_period'])));
                            $get_data[$key]['lease_submissions'][$key_i]['reporting_period'] = date("Y-m-d", strtotime($get_data[$key]['lease_submissions'][$key_i]['reporting_period']));
                        }
                    }
                }
                $leases = $this->Leases->patchEntities($leases, $get_data);
                echo "<pre>";
                var_dump($leases);
                echo "</pre>";
                die();
                $has_errors = false;
                foreach ($leases as $lease) {
                    if (!empty($lease->errors())) {
                        $has_errors = true;
                        $this->Flash->error('Lease reading could not be saved. Please check for any errors and try again.', 'flash', ['clear' => true]);
                        break;
                    }
                }

样本提交

array(2) {
    ["id"]=>
    string(3) "805"
    ["lease_submissions"]=>
    array(1) {
      [0]=>
      array(4) {
        ["lease_id"]=>
        string(3) "805"
        ["reporting_period"]=>
        string(10) "2021-09-01"
        ["num_bbls"]=>
        string(6) "209598"
        ["comments"]=>
        string(3) "dog"
      }
    }
  }

示例记录已保存

object(App\Model\Entity\LeaseSubmission)#305 (12) {
        ["lease_id"]=>
        int(805)
        ["reporting_period"]=>
        object(Cake\I18n\FrozenDate)#330 (3) {
          ["date"]=>
          string(26) "0169-05-08 00:00:00.000000"
          ["timezone_type"]=>
          int(3)
          ["timezone"]=>
          string(3) "UTC"
        }
        ["num_bbls"]=>
        float(209598)
        ["comments"]=>
        string(3) "dog"

如您所见,reporting_period 已设置为 epoc。我很难过,欢迎任何帮助。

答案是将 bootstrap.php 中的行更改为此。

Type::build('date')->useLocaleParser()->setLocaleFormat('yyyy/m/d');

并确保它在整个站点中保持一致。

谢谢nbm你的评论让我走上了正确的道路。