You might benefit from studying Doctrine2. While it also uses the "Repository Pattern", it also uses the "Data Mapper Pattern".
The following is an example of using the entity manager:
/* $movieRepository = MovieRepository -> extends EntityRepository -> implements ObjectRepository */
if (null !== $movie = $movieRepository->findOneBy(array('name' => 'Mad Max'))) {
$movie = new Movie();
$movie->setName('Mad Max');
/* $entityManager = EntityManager -> implements ObjectManager */
$entityManager->persist($movie);
}
$movie->setViews($movie->getViews() + 1);
$entityManager->flush();
Notice here that Doctrine2 defines two major classes (one abstract).
abstract class EntityRepository implements ObjectRepository
{
...
}
class EntityManager implements ObjectManager
{
...
}
Traditionally the repository is only used to make queries, i.e. for selections, rarely updates or inserts. ObjectRepository
interface defines common select methods:
find($id)
findBy(array $criteria)
findOneBy(array $criteria)
The entity manager however, has the methods pertaining to persistance:
persist($entity)
flush($entity = null)
The entity manager never requires a specific type of entity besides the "range" of classes registered as being mapped to it's database.
If you want to create your own persistence library, you could use the Doctrine\Common\Persistance
interfaces to help you design it.
ObjectRepository
ObjectManager
A reason you might choose to persist
entities and flush
all at once, is to create a coherent transaction which "finalizes" the process. Opposed to "saving" each entity individually during the process, which would gradually build up the amount of IO in your application, effecting performance.
TL;DR
Incorporate the Data Mapper Pattern in your design, and migrate the persistence away from your repositories, and instead encapsulate it into a single manager for your database. Then limit the repository to selection queries.
Item
specification, and check that parameter has the typeMovie
.