Cannot save time field in cakephp 4

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!

Hi Guys, anybody home? :slight_smile:

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.

Hi,

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

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

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’);

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

you can change it by adding in bootstrap.php

// 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');
1 Like

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?

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

I see, thanks again!

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