I have a bunch of repositories, and I often use methods like findOneBySomeField
, findByAnotherField
. These methods are not present in repository class but Doctrine can properly work with this.
And here is the problem: as these methods are not present in the repository class there are no typehints for arguments and return types.
I tried to add some PHPSTORM_META, like this:
namespace PHPSTORM_META {
override(
AppRepositoryMyEntityRepository::findOneByCode(0), map([
'' => 'AppEntityMyEntity'
])
);
}
But this doesn’t work. Moreower, even if I can make it works, I need manually add all possible methods for findBy...
and findOneBy...
. But I have dosens (50+ in some entities) of fields in some entityes, so I need too many override
directives. And I still don’t see any way to add typehints for args. It theoretically possible, because if I have string $code
in my entity I should have findOneByCode(string $code)
(argument type must be the same as the entity field type) and so on.
Is there a simple way to do that?
2
Answers
You can use Doctrine extensions for PHPStan
Add
@method
PHPDoc tags to the repository class.