Git Product home page Git Product logo

Comments (6)

driehle avatar driehle commented on June 2, 2024 2

Version 3.1.0 of doctrine-laminas-hydrator has just been released!

from doctrine-laminas-hydrator.

driehle avatar driehle commented on June 2, 2024 1

Thanks for reporting this issue. Indeed, such hydration is not handled properly, mostly because backed enum typed properties are a rather new feature of Doctrine. Could you please provide a PR including the fix and relevant test cases based on the 2.2.x branch? I'll be happy to review and merge your PR in the 2.2 and 3.0 series.

from doctrine-laminas-hydrator.

driehle avatar driehle commented on June 2, 2024 1

This is a common issue in the hydrator library, we have the same issue for isNullable:

private function isNullable(string $name): bool
{
$metadata = $this->getClassMetadata();
//TODO: need update after updating isNullable method of Doctrine\ORM\Mapping\ClassMetadata
if ($metadata->hasField($name)) {
return method_exists($metadata, 'isNullable') && $metadata->isNullable($name);
}
if ($metadata->hasAssociation($name) && method_exists($metadata, 'getAssociationMapping')) {
$mapping = $metadata->getAssociationMapping($name);
return $mapping !== false && isset($mapping['nullable']) && $mapping['nullable'];
}
return false;
}

Here isNullable() is only present in Doctrine\ORM\Mapping\ClassMetadata (inherited from Doctrine\ORM\Mapping\ClassMetadataInfo), but not in Doctrine\Persistence\Mapping\ClassMetadata. Same for getAssociationMapping().

I would suggest using a similar approach for getFieldMapping().

from doctrine-laminas-hydrator.

HeyRatFans avatar HeyRatFans commented on June 2, 2024

I have been looking into adding enum support on the side too (unofficially of course - having been playing around with 8.1's enums and Doctrine's new enum and attribute support).

Unless I am mistaken, @ezkimo I do not think your suggestion will work for hydration?

My understanding is that to accomplish hydration for enums we would need access to the ClassMetadataInfo::getFieldMapping() method to be able to determine the enumType from the attributes/annotations of the field itself, at least in the case of a non-typed setters and properties.

Unfortunately, this method is not defined on the ClassMetadata interface in Doctrine/Persistence, only in the concrete implementation in Doctrine/ORM (and some but not all the ODM packages!) and as such would need to be added upstream in Doctrine/Persistence. I am uncertain whether doing this would be a breaking change or not.

An alternative option could be to use Reflection to get the enum class name and use BackedEnum::tryFrom(), but this would enforce the use of typed setters and properties (for both by-reference and by-value hydration) to be used in the target Entity. This is not a terrible thing IMO.

from doctrine-laminas-hydrator.

HeyRatFans avatar HeyRatFans commented on June 2, 2024

It looks like this would work, but would be untestable due to PHPUnit rightfully not allowing us to mock getFieldMapping on Doctrine\Persistence\Mapping\ClassMetadata.

I think @ezkimo solution of requiring a Strategy for each enum be implemented coupled with something like the below to DoctrineObject::hydrateValue could be a solution (note: returning the enum instead of enum->value here as previously proposed)

if (interface_exists('BackedEnum') && $value instanceof BackedEnum) {
    return $value;
}

The only other option would be the previously use of Reflection, which would only work by ignoring any attributes/annotations and trusting the entity has the correctly typed property/setter on the entity class.

from doctrine-laminas-hydrator.

driehle avatar driehle commented on June 2, 2024

Since Enums have never been supported till now, we will consider this a new feature and bring it into the upcoming 3.1.0 release. However, we will need someone to write a few notes for the docs (see #57, #59).

from doctrine-laminas-hydrator.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.