Here are some snippets. I tried to remove some of the boring code. I started out fully baking the code and then edited from there. It’s pretty generic. I left the debug() statements in and included what they returned. I got tired continually fixing the date/time in the database so I just crash it before the save().
CONTROLLER:
public function edit($id = null)
{
$userGroups = $this->Auth->user('user_group_id');
$race = $this->Races->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$race = $this->Races->patchEntity($race, $this->request->getData());
debug($race->date); <- #3
crashit(); <- Just used to crash the code so it doesn't save the wrong date/time
if ($this->Races->save($race)) {
$this->Flash->success(__('The race has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The race could not be saved. Please, try again.'));
}
$racetracks = $this->Races->Racetracks->find('list', [
'keyFields' => 'id',
'valueField' => 'name',
'limit' => 200
]);
if ($racetracks->count() === 0) {
return $this->redirect(['action' => 'index']);
}
$racecars = $this->Races->Racecars->find('list', [
'conditions' => ['Racecars.user_group_id' => $userGroups],
'keyFields' => 'id',
'valueField' => 'description',
'limit' => 200
]);
if ($racecars->count() === 0) {
return $this->redirect(['action' => 'index']);
}
$users = $this->Races->Users->find('list', [
'keyFields' => 'id',
'valueField' => 'name',
'limit' => 200
]);
debug($race->date); <- #1
$race->date = $race->date->setTimezone('America/New_York');
debug($race->date); <- #2
$this->set(compact('race', 'racetracks', 'racecars', 'users'));
$this->set('userGroups', $userGroups);
}
DEBUG RESULTS (from above):
#1 (Correct)
object(Cake\I18n\FrozenTime) {
‘time’ => ‘2018-12-02T01:37:00+00:00’,
‘timezone’ => ‘UTC’,
‘fixedNowTime’ => false
}
#2 (Correct)
object(Cake\I18n\FrozenTime) {
‘time’ => ‘2018-12-01T20:37:00-05:00’,
‘timezone’ => ‘America/New_York’,
‘fixedNowTime’ => false
}
#3 (Wrong)
object(Cake\I18n\FrozenTime) {
‘time’ => ‘2018-12-01T20:37:00+00:00’,
‘timezone’ => ‘UTC’,
‘fixedNowTime’ => false
}
VIEW:
<div class="races form large-9 medium-8 columns content">
<?= $this->Form->create($race) ?>
<fieldset>
<legend><?= __('Edit Race') ?></legend>
<?php
echo $this->Form->control('date', ['label' => 'Date']);
echo $this->Form->control('rnd', ['label' => 'Rnd']);
echo $this->Form->control('elim', [
'options' => [
'Y' => 'Yes',
'N' => 'No'
],
'label' => 'Elim',
'type' => 'select'
]);
...(removed some boring stuff)...
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
TABLE:
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->dateTime('date')
->requirePresence('date', 'create')
->notEmpty('date');
$validator
->integer('rnd')
->requirePresence('rnd', 'create')
->notEmpty('rnd');
...(removed some boring stuff)...
return $validator;
}