mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
EntityManager::find return type is just object in the lexer. #5782
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @axelcho on GitHub (Nov 22, 2017).
Originally assigned to: @Ocramius on GitHub.
I would like to force strict class check on find return. It is theoretically possible to return anything as a result of EntityManger::find
Also, on EntityManager::persist($entity), there is no type hint on $entity, and theoretically I can pass any object to the entity.
@coudenysj commented on GitHub (Nov 22, 2017):
A good solution is to use Repositories (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#custom-repositories) for that.
You can implement a "find" method there (which is basically the same as doing it in the EM).
As for the persist type hint, you could add a "save" method to the Repository with a type hint.
@Ocramius commented on GitHub (Nov 22, 2017):
@axelcho this is because the ORM actually accepts any object, and will fail only when the given object is not mapped.
This is by design, as PHP has no better type system to work with.
Closing as
invalid@Majkl578 commented on GitHub (Dec 6, 2017):
Note: This will be improved by adding native
objecttype in develop/3.0 where we already require PHP 7.2. Siínce PHP has no generics, nothing better could be done here.