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
}
- ORM (Relational databases) with doctrine-orm-repositories
- MongoDB with doctrine-mongodb-odm-repositories
- CouchDB with doctrine-couchdb-odm-repositories (not maintained)
These are the new methods that juliangut/doctrine-base-repositories
brings to the table thanks to RepositoryTrait
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);
$repository->remove($managedObject);
FindAll and then removes them all.
$repository = $manager->getRepository(ObjectClass::class);
$repository->removeAll();
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->removeByActive(false);
$repository->removeOneBy(['id' => 1]);
$repository->removeOneById(1);
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
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
You might want to temporarily disable an event subscriber.
$repository = $manager->getRepository(ObjectClass::class);
$repository->disableEventSubscriber(\Gedmo\Timestampable\TimestampableListener::class);
$repository->save(new EntityClass());
$repository->restoreEventSubscribers();
You might want to disable all listeners on a certain event.
$repository = $manager->getRepository(ObjectClass::class);
$repository->disableEventListeners('onFlush');
$repository->save(new EntityClass());
$repository->restoreEventListeners('onFlush');
// $repository->restoreAllEventListener();
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->restoreEventListener('onFlush');
// $repository->restoreAllEventListener();
Filters managing is provided by FiltersTrait
You might want to temporarily disable all filters.
$repository = $manager->getRepository(ObjectClass::class);
$repository->disableFilters();
$repository->save(new EntityClass());
$repository->restoreFilters();
You might want to disable a single filter.
$repository = $manager->getRepository(ObjectClass::class);
$repository->disableFilter('locale');
$repository->save(new EntityClass());
$repository->restoreFilter('locale');
// $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 CONTRIBUTING.md
See file LICENSE included with the source code for a copy of the license terms.