DDC-3153: DoctrineObject Hydrator - handleTypeConversion interfers with date strategies #3912

Closed
opened 2026-01-22 14:31:18 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 5, 2014).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user Qronicle:

We would like to add a strategy for date properties. (So that we have automatic conversion to a \DateTime object after saving a form.)

The DoctrineObject hydrator tries to do this automatically with the handleTypeConversions method, which converts a string in a date property to a \DateTime object. Problem here is that the input format is not always the expected format for this conversion, so an exception is thrown.

We've made a DateTime strategy that converts the value to a \DateTime object with a defined format.

Problem: The handleTypeConversions method is called before the strategy is invoked, thus we get the datetime conversion exception.

Solution: We have overridden the DoctrineObject hydrator for now, and only execute the handleTypeConversions after the strategy:

    protected function hydrateByValue(array $data, $object)
    {
        $tryObject = $this->tryConvertArrayToObject($data, $object);
        $metadata  = $this->metadata;

        if (is_object($tryObject)) {
            $object = $tryObject;
        }

        foreach ($data as $field => $value) {
            $setter = 'set' . ucfirst($field);

            if ($metadata->hasAssociation($field)) {
                $target = $metadata->getAssociationTargetClass($field);

                if ($metadata->isSingleValuedAssociation($field)) {
                    if (! method_exists($object, $setter)) {
                        continue;
                    }

                    $value = $this->toOne($target, $this->hydrateValue($field, $value, $data));

                    if (null === $value
                        && !current($metadata->getReflectionClass()->getMethod($setter)->getParameters())->allowsNull()
                    ) {
                        continue;
                    }
                    $object->$setter($value);
                } elseif ($metadata->isCollectionValuedAssociation($field)) {
                    $this->toMany($object, $field, $target, $value);
                }
            } else {
                if (! method_exists($object, $setter)) {
                    continue;
                }
                $value = $this->hydrateValue($field, $value, $data);
                $value = $this->handleTypeConversions($value, $metadata->getTypeOfField($field));
                $object->$setter($value);
            }
        }

        return $object;
    }

I don't know if this is the best solution, but it works for us, anyway.

Originally created by @doctrinebot on GitHub (Jun 5, 2014). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user Qronicle: We would like to add a strategy for date properties. (So that we have automatic conversion to a \DateTime object after saving a form.) The DoctrineObject hydrator tries to do this automatically with the handleTypeConversions method, which converts a string in a date property to a \DateTime object. Problem here is that the input format is not always the expected format for this conversion, so an exception is thrown. We've made a DateTime strategy that converts the value to a \DateTime object with a defined format. Problem: The handleTypeConversions method is called before the strategy is invoked, thus we get the datetime conversion exception. Solution: We have overridden the DoctrineObject hydrator for now, and only execute the handleTypeConversions after the strategy: ``` php protected function hydrateByValue(array $data, $object) { $tryObject = $this->tryConvertArrayToObject($data, $object); $metadata = $this->metadata; if (is_object($tryObject)) { $object = $tryObject; } foreach ($data as $field => $value) { $setter = 'set' . ucfirst($field); if ($metadata->hasAssociation($field)) { $target = $metadata->getAssociationTargetClass($field); if ($metadata->isSingleValuedAssociation($field)) { if (! method_exists($object, $setter)) { continue; } $value = $this->toOne($target, $this->hydrateValue($field, $value, $data)); if (null === $value && !current($metadata->getReflectionClass()->getMethod($setter)->getParameters())->allowsNull() ) { continue; } $object->$setter($value); } elseif ($metadata->isCollectionValuedAssociation($field)) { $this->toMany($object, $field, $target, $value); } } else { if (! method_exists($object, $setter)) { continue; } $value = $this->hydrateValue($field, $value, $data); $value = $this->handleTypeConversions($value, $metadata->getTypeOfField($field)); $object->$setter($value); } } return $object; } ``` I don't know if this is the best solution, but it works for us, anyway.
admin added the Bug label 2026-01-22 14:31:18 +01:00
admin closed this issue 2026-01-22 14:31:19 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jun 6, 2014):

Comment created by @ocramius:

This bug should be reported at https://github.com/doctrine/DoctrineModule, as it doesn't affect the core ORM

@doctrinebot commented on GitHub (Jun 6, 2014): Comment created by @ocramius: This bug should be reported at https://github.com/doctrine/DoctrineModule, as it doesn't affect the core ORM
Author
Owner

@doctrinebot commented on GitHub (Jun 6, 2014):

Issue was closed with resolution "Can't Fix"

@doctrinebot commented on GitHub (Jun 6, 2014): Issue was closed with resolution "Can't Fix"
Author
Owner

@doctrinebot commented on GitHub (Jun 6, 2014):

Comment created by Qronicle:

Will do, thanks for pointing me in the right direction!

@doctrinebot commented on GitHub (Jun 6, 2014): Comment created by Qronicle: Will do, thanks for pointing me in the right direction!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3912