June 2017
Intermediate to advanced
394 pages
8h 52m
English
interface PostRepository { // ... public function size(); }
The implementation could look like this:
class DoctrinePostRepository implements PostRepository{ // ... public function size() { return $this->em->createQueryBuilder() ->select('count(p.id)') ->from('Domain\Model\Post', 'p') ->getQuery() ->getSingleScalarResult(); }}
Adding additional behavior to a Repository can be very beneficial. An example of this is the ability to count all the items in a given collection. You might think to add a method with the name count; however, as we're trying to mimic a collection, a better name would instead be size:
You're also able to place specific calculations, counters, read-optimized queries, or complex commands (INSERT,