active-record
active-record copied to clipboard
Bug new values incorrect detection
| Q | A |
|---|---|
| Is bugfix? | ✔️ |
| New feature? | ❌ |
| Breaks BC? | ✔️ |
| Fixed issues | #522 |
Problem
AbstractActiveRecord::newValues() uses strict comparison (!==) also for DateTimeInterface attributes,
which causes identical date/time values to be marked as changed because they are different objects.
Solution
If the new value implement DateTimeInterface, compare both objects by comparing the formatted date Y-m-d\TH:i:s.uP, e.g.: 2011-02-03T04:05:06.123456+01:00.
This fixes false positives and works also when two different date time objects represents the same moment in time but have different time zones.
E.g.:
- 2011-01-01T01:01:01.111111+01:00
- 2011-01-01T00:01:01.111111+00:00
... they both represent the same moment in time, but the formatted string is clearly different and thus can be detected as changed by the newValues() function.
Tests
Added tests to cover:
- identical DateTime values not marked as dirty
- identical moment in time values marked as dirty, because of different time zone
- different DateTime values correctly detected as changes