I have an entity with timestamp behavior.
Originaly, I found that data in database was never good for the created field.
I thought of some date formating issues, or the timestamp behavior having something broken.
However I was able to narrow down the possibilities to the beforeSave method, where I have some business code that split an array containing 2 dates into 2 fields for DB insertion.
In fact, when accessing this date field, the created date would somehow update to the same value of that field… WTF ?
Does someone have an idea of what is going on ? This looks to me like a CakePHP related bug ?
This is my beforeSave (results speak more than my sentences) :
public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options)
{
var_dump($entity);
/* Partly reproduced :
public 'created' =>
object(Cake\I18n\Time)[612]
public 'time' => string '2016-10-13T10:05:01+00:00' (length=25)
public 'timezone' => string 'UTC' (length=3)
public 'fixedNowTime' => boolean false
public 'modified' =>
object(Cake\I18n\Time)[617]
public 'time' => string '2016-10-13T10:05:01+00:00' (length=25)
public 'timezone' => string 'UTC' (length=3)
public 'fixedNowTime' => boolean false
*/
var_dump($entity->decision_date);
/* Results in :
array (size=4)
'min' =>
object(Cake\I18n\Time)[612]
public 'time' => string '2017-07-13T10:05:01+00:00' (length=25)
public 'timezone' => string 'UTC' (length=3)
public 'fixedNowTime' => boolean false
'max' =>
object(Cake\I18n\Time)[612]
public 'time' => string '2017-07-13T10:05:01+00:00' (length=25)
public 'timezone' => string 'UTC' (length=3)
public 'fixedNowTime' => boolean false
'sameYear' => boolean true
'past' => boolean false
*/
var_dump($entity);
/* Partly reproduced :
public 'created' =>
object(Cake\I18n\Time)[612]
public 'time' => string '2017-07-13T10:05:01+00:00' (length=25)
public 'timezone' => string 'UTC' (length=3)
public 'fixedNowTime' => boolean false
public 'modified' =>
object(Cake\I18n\Time)[617]
public 'time' => string '2016-10-13T10:01:53+00:00' (length=25)
public 'timezone' => string 'UTC' (length=3)
public 'fixedNowTime' => boolean false
*/
/* The rest of business code was here... But problem persist with only var_dump in the method... */
}
Originally posted on StackOverflow here : http://stackoverflow.com/questions/39998361/cakephp-3-wrong-date-inserted-in-db-timestamp-behavior
EDIT - Issue Resolved :
I am working on someone else’s code, and the method _getDecisionDate() had been overridden by the original developer.
In fact, he was using directly created field to set DecisionDate to the right value in the Entity file…
$dateMax = $this->created->modify('+ 2 years');
So problem slved in the end, this wasn’t a big deal but this gave me headache for 1 day