juliangut/doctrine-base-repositories

Doctrine2 utility repositories

1.1.2 2018-06-05 21:24 UTC

This package is auto-updated.

Last update: 2024-11-15 11:57:14 UTC


README

PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads

doctrine-base-repositories

Doctrine2 utility repositories. Use as a base for custom repositories

Installation

Composer

composer require juliangut/doctrine-base-repositories

Use

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
}

Implementations

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');

Creating

getNew

Creates a new empty object directly from repository.

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

$newObject = $repository->getNew();

findOneByOrGetNew

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']);

Adding

add

Will persist the entity into the manager.

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

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

Removing

remove

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

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

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

$repository->remove($managedObject);

removeAll

FindAll and then removes them all.

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

$repository->removeAll();

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->removeByActive(false);
$repository->removeOneBy(['id' => 1]);
$repository->removeOneById(1);

Counting

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->disableEventSubscriber(\Gedmo\Timestampable\TimestampableListener::class);
$repository->save(new EntityClass());
$repository->restoreEventSubscribers();

Disabling an event listeners

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();

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->restoreEventListener('onFlush');
// $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->disableFilters();
$repository->save(new EntityClass());
$repository->restoreFilters();

Disabling a single filter

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

Paginating

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

Contributing

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

See file CONTRIBUTING.md

License

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