Closed Hermaniandr closed 3 weeks ago
Why should it return null? Doesn't an exception make more sense than checking the return value every time.
I've addressed this so an exception is returned if class is not found in any entity manager.
@TomHAnderson Doctrine doesn't throw exception in case manager not found - it just returns null
, and some packages rely on this behavior. As an example - https://github.com/zenstruck/foundry, when it goes through Entity properties, it tries to find managed if property is object
- if manager found, it treats property as Entity (persist & some other actions), otherwise - as simple Object. Here link where it happens - link - internally it calls following function, so throwing exception in getManagerForClass()
won't allow to use package.
I faced this issue when integrating zenstruck/foundry
. Fortunately, I have two connections, so with second connection configured it was working as expected. But as far as I see with new changes it won't work :anguished:
I'm pretty sure there might be some other doctrine-related packages that rely on null
, and throwing exception won't allow to use them.
For me also makes sense to have a method that guarantees that Manager will be returned (via throwing exception), but probably better to follow doctrine's behavior, so we won't lose possibility to integrate doctrine-related packages.
https://github.com/laravel-doctrine/orm/pull/644
@Hermaniandr Please inspect this change. I believe it covers what was and what we're both looking for.
@TomHAnderson looks great, left a minor comment regarding return annotation. Thanks!
If there is only one manager, there is no check that it supports entity - it was removed in this PR https://github.com/laravel-doctrine/orm/pull/288/files As a result, if method called with some random class (e.g.
\DateTime
) - manager will be returned, even though it doesn't support this class.Package version, Laravel version
laravel-doctrine/orm: 2.1.0 laravel/framework: 11.9.2
Expected behaviour
LaravelDoctrine\ORM\IlluminateRegistry::getManagerForClass
returnsnull
for non-Entities if it has one manager.Actual behaviour
LaravelDoctrine\ORM\IlluminateRegistry::getManagerForClass
returns manager for non-entities if it has one manager.Steps to reproduce the behaviour
Registry::getManagerForClass(\DateTime::class)