Menu

1 / 2

Your Doctrine repositories don't need to extend anything

DEV Community: laravel·delacry·5 days ago
#rw7v1vJl
#devtolaravel#devto#dev#product#repository
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.