File tree Expand file tree Collapse file tree 2 files changed +45
-4
lines changed
Extension/Core/DataMapper
Tests/Extension/Core/DataMapper Expand file tree Collapse file tree 2 files changed +45
-4
lines changed Original file line number Diff line number Diff line change @@ -73,16 +73,17 @@ public function mapFormsToData($forms, &$data)
73
73
// Write-back is disabled if the form is not synchronized (transformation failed),
74
74
// if the form was not submitted and if the form is disabled (modification not allowed)
75
75
if (null !== $ propertyPath && $ config ->getMapped () && $ form ->isSubmitted () && $ form ->isSynchronized () && !$ form ->isDisabled ()) {
76
- // If the field is of type DateTime and the data is the same skip the update to
76
+ $ propertyValue = $ form ->getData ();
77
+ // If the field is of type DateTime or DateTimeInterface and the data is the same skip the update to
77
78
// keep the original object hash
78
- if ($ form -> getData () instanceof \DateTime && $ form -> getData () == $ this ->propertyAccessor ->getValue ($ data , $ propertyPath )) {
79
+ if (( $ propertyValue instanceof \DateTime || $ propertyValue instanceof \DateTimeInterface) && $ propertyValue == $ this ->propertyAccessor ->getValue ($ data , $ propertyPath )) {
79
80
continue ;
80
81
}
81
82
82
83
// If the data is identical to the value in $data, we are
83
84
// dealing with a reference
84
- if (!\is_object ($ data ) || !$ config ->getByReference () || $ form -> getData () !== $ this ->propertyAccessor ->getValue ($ data , $ propertyPath )) {
85
- $ this ->propertyAccessor ->setValue ($ data , $ propertyPath , $ form -> getData () );
85
+ if (!\is_object ($ data ) || !$ config ->getByReference () || $ propertyValue !== $ this ->propertyAccessor ->getValue ($ data , $ propertyPath )) {
86
+ $ this ->propertyAccessor ->setValue ($ data , $ propertyPath , $ propertyValue );
86
87
}
87
88
}
88
89
}
Original file line number Diff line number Diff line change @@ -357,4 +357,44 @@ public function testMapFormsToDataIgnoresDisabled()
357
357
358
358
$ this ->mapper ->mapFormsToData (array ($ form ), $ car );
359
359
}
360
+
361
+ /**
362
+ * @dataProvider provideDate
363
+ */
364
+ public function testMapFormsToDataDoesNotChangeEqualDateTimeInstance ($ date )
365
+ {
366
+ $ article = array ();
367
+ $ publishedAt = $ date ;
368
+ $ article ['publishedAt ' ] = clone $ publishedAt ;
369
+ $ propertyPath = $ this ->getPropertyPath ('[publishedAt] ' );
370
+
371
+ $ this ->propertyAccessor ->expects ($ this ->once ())
372
+ ->method ('getValue ' )
373
+ ->willReturn ($ article ['publishedAt ' ])
374
+ ;
375
+ $ this ->propertyAccessor ->expects ($ this ->never ())
376
+ ->method ('setValue ' )
377
+ ;
378
+
379
+ $ config = new FormConfigBuilder ('publishedAt ' , \get_class ($ publishedAt ), $ this ->dispatcher );
380
+ $ config ->setByReference (false );
381
+ $ config ->setPropertyPath ($ propertyPath );
382
+ $ config ->setData ($ publishedAt );
383
+ $ form = $ this ->getForm ($ config );
384
+
385
+ $ this ->mapper ->mapFormsToData (array ($ form ), $ article );
386
+ }
387
+
388
+ public function provideDate ()
389
+ {
390
+ $ data = array (
391
+ '\DateTime ' => array (new \DateTime ()),
392
+ );
393
+
394
+ if (class_exists ('DateTimeImmutable ' )) {
395
+ $ data ['\DateTimeImmutable ' ] = array (new \DateTimeImmutable ());
396
+ }
397
+
398
+ return $ data ;
399
+ }
360
400
}
You can’t perform that action at this time.
0 commit comments