bug #10151 [Form] Update DateTime objects only if the actual value has changed (peterrehm)

This PR was squashed before being merged into the 2.3 branch (closes #10151).

Discussion
----------

[Form] Update DateTime objects only if the actual value has changed

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Right now the Form component replaces DateTime fields with new Objects, therefore the hash is changing.
This leads to unnecessary database updated when working with doctrine. With this patch the DateTime object
of the actual entity is only replaced if the value has been changed.

Commits
-------

1f22d3a [Form] Update DateTime objects only if the actual value has changed
This commit is contained in:
Fabien Potencier 2014-01-29 07:46:08 +01:00
commit e973fa45f5
1 changed files with 7 additions and 0 deletions

View File

@ -81,6 +81,13 @@ class PropertyPathMapper implements DataMapperInterface
// Write-back is disabled if the form is not synchronized (transformation failed),
// if the form was not submitted and if the form is disabled (modification not allowed)
if (null !== $propertyPath && $config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled()) {
// If the field is of type DateTime and the data is the same skip the update to
// keep the original object hash
if ($form->getData() instanceof \DateTime && $form->getData() == $this->propertyAccessor->getValue($data, $propertyPath)) {
continue;
}
// If the data is identical to the value in $data, we are
// dealing with a reference
if (!is_object($data) || !$config->getByReference() || $form->getData() !== $this->propertyAccessor->getValue($data, $propertyPath)) {