Aaron Blakeley
Aaron Blakeley

Reputation: 370

Cake PHP PatchEntities saveing the epoc

I inherited a cake application and I am having some trouble with date-times stuff.

When I save a submission I have to format the date to be Y-m-d. This allows the patchEntities() to save but when I check the data base the date is the epoc.

Current Code

$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;
                    }
                }

Sample Submission

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"
      }
    }
  }

Sample record saved

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"

As you can see the reporting_period is set to the epoc. I am stumped any help is welcome.

Upvotes: 0

Views: 81

Answers (1)

Aaron Blakeley
Aaron Blakeley

Reputation: 370

The answer was to change the line in bootstrap.php to this.

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

and ensuring it was consistent throughout the site.

Thank you nbm your comment lead me down the right path.

Upvotes: 0

Related Questions