# Cannot save time field in cakephp 4

**URL:** <https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523>\
**Category:** Need Help\
**Created:** [March 23, 2020, 4:57am UTC](https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523 "2020-03-23T04:57:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpals](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jpals](https://discourse.cakephp.org/u/jpals)\
**Post date:** [March 23, 2020, 4:57am UTC](https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523/1 "2020-03-23T04:57:56Z")

</div>

Hi,

I have been tinkering with Cake 4 and I’m having trouble saving a time field. I have a table with start\_time and end\_time columns with datatype TIME. The form helper recognizes them but when I try to save it the data will not save.

Post Data:  
[  
‘event\_id’ =\> ‘2’,  
‘other\_event’ =\> ‘’,  
‘location\_id’ =\> ‘2’,  
‘start\_date’ =\> ‘2020-12-31’,  
‘start\_time’ =\> ‘09:00’,  
‘end\_date’ =\> ‘2020-12-31’,  
‘end\_time’ =\> ‘10:00’,  
‘slots’ =\> ‘123’  
]

Patched Entity:

object(App\Model\Entity\Logbook) {

```
'event_id' => (int) 2,
'other_event' => '',
'location_id' => (int) 2,
'start_date' => object(Cake\I18n\FrozenDate) {

	'time' => '2020-12-31 00:00:00.000000+00:00',
	'timezone' => 'UTC',
	'fixedNowTime' => false

},
'end_date' => object(Cake\I18n\FrozenDate) {

	'time' => '2020-12-31 00:00:00.000000+00:00',
	'timezone' => 'UTC',
	'fixedNowTime' => false

},
'slots' => (int) 123,
'[new]' => true,
'[accessible]' => [
	'event_id' => true,
	'other_event' => true,
	'location_id' => true,
	'start_date' => true,
	'start_time' => true,
	'end_date' => true,
	'end_time' => true,
	'slots' => true,
	'created' => true,
	'modified' => true,
	'event' => true,
	'location' => true,
	'participants' => true
],
'[dirty]' => [
	'event_id' => true,
	'other_event' => true,
	'location_id' => true,
	'start_date' => true,
	'end_date' => true,
	'slots' => true
],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Logbooks'

```

}

When patching, the start\_time and end\_time are no longer there. Is there a particular way of handling time fields in cakephp 4? I did not have this issue when using 3.8. Any thoughts?

Thanks!

---

<div class="post-metadata">

**Author:** ![jpals](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jpals](https://discourse.cakephp.org/u/jpals)\
**Post date:** [March 25, 2020, 6:31am UTC](https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523/2 "2020-03-25T06:31:30Z")

</div>

Hi Guys, anybody home? 🙂

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 25, 2020, 8:40pm UTC](https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523/3 "2020-03-25T20:40:16Z")

</div>

My only thought would be maybe the time fields were added later and the ORM cache doesn’t have them? But then I wouldn’t think that the form helper would know what type they should be. It all looks fine, but I haven’t started with 4.x yet.

---

<div class="post-metadata">

**Author:** ![jpals](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jpals](https://discourse.cakephp.org/u/jpals)\
**Post date:** [March 26, 2020, 4:46am UTC](https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523/4 "2020-03-26T04:46:55Z")

</div>

Hi,

Yeah, I tried that, deleted cache for that specific model but no dice. Thanks for the input though.

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [March 26, 2020, 4:50am UTC](https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523/5 "2020-03-26T04:50:33Z")

</div>

probably invalid input (did you set it in `bootstrap.php`?) and no validation for this fields so no error for patching

---

<div class="post-metadata">

**Author:** ![jpals](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jpals](https://discourse.cakephp.org/u/jpals)\
**Post date:** [March 26, 2020, 5:15am UTC](https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523/6 "2020-03-26T05:15:25Z")

</div>

Sorry, I’m not sure what you meant by setting it in bootstrap, are you referring to the cache? With regards to validation, when I baked the model it was like this:

$validator  
-\>time(‘start\_time’)  
-\>allowEmptyTime(‘start\_time’);

Tried setting it as “NotEmptyTime” but there were no errors flagged. The time it went through patchEntity, the time columns are not there.

My work around for this is by patching the request data directly:

$logbook = $this-\>Logbooks-\>patchEntity($logbook, $this-\>request-\>getData());  
$logbook-\>start\_time = $this-\>request-\>getData(‘start\_time’);  
$logbook-\>end\_time = $this-\>request-\>getData(‘end\_time’);

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [March 26, 2020, 6:26am UTC](https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523/7 "2020-03-26T06:26:32Z")

</div>

default input format is [https://github.com/cakephp/cakephp/blob/8de055e0cacefbe81bf492b9481b0d74144ca9f5/src/Database/Type/TimeType.php#L35](https://github.com/cakephp/cakephp/blob/8de055e0cacefbe81bf492b9481b0d74144ca9f5/src/Database/Type/TimeType.php#L35)

you can change it by adding in `bootstrap.php`

```auto
// for input
\Cake\Database\Type::build('time')
    ->useLocaleParser()
    ->setLocaleFormat('HH:mm');

// for output
\Cake\I18n\FrozenTime::setToStringFormat('HH:mm');
\Cake\I18n\FrozenTime::setJsonEncodeFormat('HH:mm');

```

---

<div class="post-metadata">

**Author:** ![jpals](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jpals](https://discourse.cakephp.org/u/jpals)\
**Post date:** [March 26, 2020, 6:48am UTC](https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523/8 "2020-03-26T06:48:24Z")

</div>

Oh wow, that worked! It is patching correctly now thanks so much!  
Is there a default config similar to what you gave or do I need to have to add that line in bootstrap every time I’m doing a new project that deals with time fields?

---

<div class="post-metadata">

**Author:** ![Graziel](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Graziel](https://discourse.cakephp.org/u/Graziel)\
**Post date:** [March 26, 2020, 7:46am UTC](https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523/9 "2020-03-26T07:46:01Z")

</div>

you can just create template for your future projects and push it to github and use it instead of creating new cake app each time

---

<div class="post-metadata">

**Author:** ![jpals](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jpals](https://discourse.cakephp.org/u/jpals)\
**Post date:** [March 26, 2020, 7:57am UTC](https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523/10 "2020-03-26T07:57:33Z")

</div>

I see, thanks again!

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 26, 2020, 3:30pm UTC](https://discourse.cakephp.org/t/cannot-save-time-field-in-cakephp-4/7523/11 "2020-03-26T15:30:51Z")

</div>

That’s strange. I wondered about that, but disregarded it because the `errors` list in the resulting entity was empty.
