Feature: #100071 - Introduce non-magic repository find methods¶
See forge#100071
Description¶
Extbase repositories come with a magic __call()
-method to allow calling
the following methods without implementing:
findBy[PropertyName]($propertyValue)
findOneBy[PropertyName]($propertyValue)
countBy[PropertyName]($propertyValue)
Magic methods are quite handy but they have a huge disadvantage. There is no
proper IDE support i.e. most IDEs show an error or at least a warning,
saying method findByAuthor()
does not exist. Also, type declarations are
impossible to use because with __call()
everything is mixed
. And
last but not least, static code analysis - like phpstan - cannot properly
analyse those and give meaningful errors.
Therefore, there is a new set of methods without all those downsides:
findBy(array $criteria, ...): QueryResultInterface
findOneBy(array $criteria, ...):object|null
count(array $criteria, ...): int
The naming of those methods follows those of doctrine/orm
and only
count()
differs from the formerly countBy()
. While all magic
methods only allow for a single comparison (propertyName
= propertyValue
),
those methods allow for multiple comparisons, called constraints.
Example:
$this->blogRepository->findBy(['author' => 1, 'published' => true]);
Impact¶
Those new methods support a broader feature set, support IDEs, static code analysers and type declarations.