FriendsOfSymfony / FOSRestBundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony
http://symfony.com/doc/master/bundles/FOSRestBundle/index.html
MIT License
2.79k stars 703 forks source link

EntityToIdObjectTransformer incompatible with doctrine/orm v2.8.X #2302

Closed mergeMarc closed 3 years ago

mergeMarc commented 3 years ago

Issue

The first constructor parameter of class EntityToIdObjectTransformer uses the type declaration Doctrine\Common\Persistence\ObjectManager.

This causes a TypeError to be thrown when a ObjectManager implementation that uses doctrine/persistence v2.X is passed.

Example

Error message: Argument 1 passed to FOS\RestBundle\Form\Transformer\EntityToIdObjectTransformer::__construct() must be an instance of Doctrine\Common\Persistence\ObjectManager, instance of Doctrine\ORM\EntityManager given ...

Here Doctrine\ORM\EntityManager implements Doctrine\ORM\EntityManagerInterface which in turn extends Doctrine\Persistence\ObjectManager.

Cause

The doctrine/common package was separated into multiple packages (see https://github.com/doctrine/common/issues/826). Use of the namespace Doctrine\Common\Persistence was deprecated and use of the new package namespace Doctrine\Persistence encouraged. The backwards compatibility layer for this deprecated namespace was eventually removed in v2.0.0 of doctrine/persistence (see https://github.com/doctrine/persistence/pull/106).

Both doctrine/orm v2.8.0 and doctrine/common v3.0.0, switched to version 2 of doctrine/persistence (see https://github.com/doctrine/orm/pull/8166 and https://github.com/doctrine/common/pull/890).

Mitigation

Current mitigation for developers using doctrine/orm and friendsofsymfony/rest-bundle is to restrict the dependencies in their projects as follows:

File composer.json:

"require": {
    ...
    "doctrine/common": "^2.13",
    "doctrine/orm": "2.7.*",
    "doctrine/persistence": "^1.3",
    ...
}
zatloeri commented 3 years ago

This is a huge issue for me and I think for anyone using php >=8 Doctine orm added support for php 8 in version 2.8 and thus we cannot roll back to 2.7.

I would really like to leverage some benefits of fos rest, but as of now I do not know how to get around this problem. Does anybody have any temporary workarounds to deal with this?

xabbuh commented 3 years ago

Does #2306 work for you?

mergeMarc commented 3 years ago

2306 fixes the TypeError while using doctrine/orm v2.8.4 for me.

However, I still had trouble using the Transformer due to unrelated issues: reverseTransform always returns null due to the value passed never being an array but a string (Past changes in symfony/form break functionality?). Was not able to get it working with a simple test just like the example in the docs (Maybe I’m using it wrong). Should I open an issue for that?

I ended up using form field type Symfony\Bridge\Doctrine\Form\Type\EntityType to handle validation and id to object conversion.

xabbuh commented 3 years ago

This rather sounds like your use case does not require the transformer at all if the EntityType already supports it.

GuilhemN commented 3 years ago

Closing as https://github.com/FriendsOfSymfony/FOSRestBundle/pull/2306 was merged.