This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Doctrine2 utility repositories

1.1.2 2018-06-05 21:24 UTC

This package is auto-updated.

Last update: 2021-06-15 04:28:53 UTC


PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads


Doctrine2 utility repositories. Use as a base for custom repositories



composer require juliangut/doctrine-base-repositories


Create your custom repository implementing Repository interface

use Doctrine\ORM\EntityRepository;
use Jgut\Doctrine\Repository\EventsTrait;
use Jgut\Doctrine\Repository\FiltersTrait;
use Jgut\Doctrine\Repository\PaginatorTrait;
use Jgut\Doctrine\Repository\Repository;
use Jgut\Doctrine\Repository\RepositoryTrait;

class customRepository extends EntityRepository implements Repository
    use RepositoryTrait;
    use EventsTrait;
    use FiltersTrait;
    use PaginatorTrait;

    protected function getFilterCollection()
        // Custom implementation

    public function countBy($criteria)
        // Custom implementation

    public function findPaginatedBy($criteria, array $orderBy = [], $itemsPerPage = 10)
        // Custom implementation

    protected function getManager()
        // Custom implementation

    // Custom methods


New methods

These are the new methods that juliangut/doctrine-base-repositories brings to the table thanks to RepositoryTrait

Find or fail

Same functionality as findBy, findOneBy and their "magic" combinations but throwing an exception if nothing is found

$repository = $manager->getRepository(ObjectClass::class);

$object = $repository->findByOrFail(['slug' => 'my_slug']);
$object = $repository->findBySlugOrFail('my_slug');
$object = $repository->findOneByOrFail(['slug' => 'my_slug']);
$object = $repository->findOneBySlugOrFail('my_slug');



Creates a new empty object directly from repository.

$repository = $manager->getRepository(ObjectClass::class);

$newObject = $repository->getNew();


Returns an object based on criteria or a new empty object if could not be found

$repository = $manager->getRepository(ObjectClass::class);

$existingOrNewObject = $repository->findOneByOrGetNew(['slug' => 'my_slug']);



Will persist the entity into the manager.

$repository = $manager->getRepository(ObjectClass::class);

$repository->add(new ObjectClass());



In the same fashion as add this will remove the entity.

$repository = $manager->getRepository(ObjectClass::class);

$managedObject = $repository->findById(1);



FindAll and then removes them all.

$repository = $manager->getRepository(ObjectClass::class);


removeBy and removeOneBy

As their counter parts findBy and findOneBy but removing the objects instead of returning them.

$repository = $manager->getRepository(ObjectClass::class);

$repository->removeBy(['active' => false]);
$repository->removeOneBy(['id' => 1]);


countAll and countBy

Perform object count

$repository = $manager->getRepository(ObjectClass::class);

$totalObjects = $repository->countAll();
$activeObjects = $repository->countBy(['active' => true]);
$activeObjects = $repository->countByActive(true);

countBy method needs implementation on custom repository

Events managing

It is common to have event subscribers on manager's event manager. This is usually due to the use of Doctrine extensions that add extra behaviour in certain points of the lifecycle. gedmo/doctrine-extensions is an example of such behaviours.

Events managing is provided by EventsTrait

Disabling event subscribers

You might want to temporarily disable an event subscriber.

$repository = $manager->getRepository(ObjectClass::class);

$repository->save(new EntityClass());

Disabling an event listeners

You might want to disable all listeners on a certain event.

$repository = $manager->getRepository(ObjectClass::class);

$repository->save(new EntityClass());
// $repository->restoreAllEventListener();

Disabling single event listeners

You might want to disable certain listeners and not all listeners registered for an event.

$repository = $manager->getRepository(ObjectClass::class);

$repository->disableEventListener('onFlush', \Gedmo\Loggable/LoggableListener::class);
$repository->save(new EntityClass());
// $repository->restoreAllEventListener();

Filters managing

Filters managing is provided by FiltersTrait

Disabling filters

You might want to temporarily disable all filters.

$repository = $manager->getRepository(ObjectClass::class);

$repository->save(new EntityClass());

Disabling a single filter

You might want to disable a single filter.

$repository = $manager->getRepository(ObjectClass::class);

$repository->save(new EntityClass());
// $repository->restoreFilters();

requires the implementation of getFilterCollection method on custom repository


Returns the same results that findBy would return but within a \Zend\Paginator\Paginator object with pagination information, an exception throwing version is also available. Provided by PaginatorTrait

$repository = $manager->getRepository(ObjectClass::class);

$paginator = $repository->findPaginatedBy(['active' => true], ['date' => 'ASC'], 10);
$paginator = $repository->findPaginatedByActive(true, ['date' => 'ASC'], 10);

// Assuming there are 80 "active"
$paginator->getTotalItemCount(); // 80
$paginator->getCurrentItemCount(); // 10
$paginator->getCurrentPageNumber(); // 1

$paginator = $repository->findPaginatedByOrFail(['active' => true], ['date' => 'ASC'], 10);

findPaginatedBy method needs implementation on custom repository


Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.



See file LICENSE included with the source code for a copy of the license terms.