How do you write Doctrine repositories, is it something like the code below? I've seen it so many times, unfortunately, so I decided to write about why it's not the best option.
class ProductRepository extends ServiceEntityRepository { public function __construct ( ManagerRegistry $registry ) { parent :: __construct ( $registry , Product :: class ); } }
Enter fullscreen mode
Exit fullscreen mode
For some, it's muscle memory, make:entity generates it, every codebase has it and nobody questions it too much. The extends gets you find() , findBy() , findOneBy() , findAll() , count() and createQueryBuilder() without writing them, I don't think that's worth what it costs.
Every finder is public
A repository is supposed to be the one place that knows how to query an entity, so commands and controllers share that logic.…