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)
7373 // Write-back is disabled if the form is not synchronized (transformation failed),
7474 // if the form was not submitted and if the form is disabled (modification not allowed)
7575 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
7778 // 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 )) {
7980 continue ;
8081 }
8182
8283 // If the data is identical to the value in $data, we are
8384 // 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 );
8687 }
8788 }
8889 }
Original file line number Diff line number Diff line change @@ -357,4 +357,44 @@ public function testMapFormsToDataIgnoresDisabled()
357357
358358 $ this ->mapper ->mapFormsToData (array ($ form ), $ car );
359359 }
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+ }
360400}
You can’t perform that action at this time.
0 commit comments