PHPackages                             juliangut/doctrine-base-repositories - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. juliangut/doctrine-base-repositories

ActiveLibrary[Database &amp; ORM](/categories/database)

juliangut/doctrine-base-repositories
====================================

Doctrine2 utility repositories

1.1.2(8y ago)337212MITPHPPHP ^7.0

Since Oct 16Pushed 8y ago1 watchersCompare

[ Source](https://github.com/juliangut/doctrine-base-repositories)[ Packagist](https://packagist.org/packages/juliangut/doctrine-base-repositories)[ Docs](https://github.com/juliangut/doctrine-base-repositories)[ RSS](/packages/juliangut-doctrine-base-repositories/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)Dependencies (16)Versions (7)Used By (2)

[![PHP version](https://camo.githubusercontent.com/32bb67dea4c68d1440c69bedc3be92e8b0c45a4e957c7048c72a3293ea05d6be/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253345253344372e302d3838393242462e7376673f7374796c653d666c61742d737175617265)](http://php.net)[![Latest Version](https://camo.githubusercontent.com/f68d39d0f3430a7b15226c5bc58ab1ca8922531b0218488546f2f426c942c120/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f6a756c69616e6775742f646f637472696e652d626173652d7265706f7369746f726965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/juliangut/doctrine-base-repositories)[![License](https://camo.githubusercontent.com/e6d3648087cb7ccb3620b89b7a9f70cd5ab20090b4893eacdad9ed35eafbcfd8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a756c69616e6775742f646f637472696e652d626173652d7265706f7369746f726965732e7376673f7374796c653d666c61742d737175617265)](https://github.com/juliangut/doctrine-base-repositories/blob/master/LICENSE)

[![Build Status](https://camo.githubusercontent.com/c7863442fcc8ae7874b9ccc56e7a044aead167275a980c37752d3b5794edf856/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6a756c69616e6775742f646f637472696e652d626173652d7265706f7369746f726965732e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/juliangut/doctrine-base-repositories)[![Style Check](https://camo.githubusercontent.com/29b101dc80d0fcc49eb364e46a648fac0a7e8060c505876bef918c3689d58588/68747470733a2f2f7374796c6563692e696f2f7265706f732f36393736333930322f736869656c64)](https://styleci.io/repos/69763902)[![Code Quality](https://camo.githubusercontent.com/07917ed3182aeb97f78b80542b7de2887be49ba77d1643b7cfa524a6dd6a8dde/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6a756c69616e6775742f646f637472696e652d626173652d7265706f7369746f726965732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/juliangut/doctrine-base-repositories)[![Code Coverage](https://camo.githubusercontent.com/0e9624cb43cb020d24a94181013c03128580aa52039a62f290de9815d4cfca54/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6a756c69616e6775742f646f637472696e652d626173652d7265706f7369746f726965732e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/github/juliangut/doctrine-base-repositories)

[![Total Downloads](https://camo.githubusercontent.com/fee182417e9c2137818fae5abcd74ea10eef9b01c17272af94a2d3e44481c09d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a756c69616e6775742f646f637472696e652d626173652d7265706f7369746f726965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/juliangut/doctrine-base-repositories)[![Monthly Downloads](https://camo.githubusercontent.com/8c28e3c6ec55781dd4c215bcfa668a7858dd3e5b25fd4bf9ba4d4f7f9d10d2e7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f6a756c69616e6775742f646f637472696e652d626173652d7265706f7369746f726965732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/juliangut/doctrine-base-repositories)

doctrine-base-repositories
==========================

[](#doctrine-base-repositories)

Doctrine2 utility repositories. Use as a base for custom repositories

Installation
------------

[](#installation)

### Composer

[](#composer)

```
composer require juliangut/doctrine-base-repositories

```

Use
---

[](#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

[](#implementations)

- ORM (Relational databases) with [doctrine-orm-repositories](https://github.com/juliangut/doctrine-orm-repositories)
- MongoDB with [doctrine-mongodb-odm-repositories](https://github.com/juliangut/doctrine-mongodb-odm-repositories)
- CouchDB with [doctrine-couchdb-odm-repositories](https://github.com/juliangut/doctrine-couchdb-odm-repositories) *(not maintained)*

New methods
-----------

[](#new-methods)

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

### Find or fail

[](#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

[](#creating)

#### getNew

[](#getnew)

Creates a new empty object directly from repository.

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

$newObject = $repository->getNew();
```

#### findOneByOrGetNew

[](#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

[](#adding)

#### add

[](#add)

Will persist the entity into the manager.

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

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

### Removing

[](#removing)

#### remove

[](#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

[](#removeall)

FindAll and then removes them all.

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

$repository->removeAll();
```

#### removeBy and removeOneBy

[](#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

[](#counting)

#### countAll and countBy

[](#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
---------------

[](#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](https://github.com/Atlantic18/DoctrineExtensions) is an example of such behaviours.

Events managing is provided by `EventsTrait`

### Disabling event subscribers

[](#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

[](#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

[](#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)

Filters managing is provided by `FiltersTrait`

### Disabling filters

[](#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

[](#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
----------

[](#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
------------

[](#contributing)

Found a bug or have a feature request? [Please open a new issue](https://github.com/juliangut/doctrine-base-repositories/issues). Have a look at existing issues before.

See file [CONTRIBUTING.md](https://github.com/juliangut/doctrine-base-repositories/blob/master/CONTRIBUTING.md)

License
-------

[](#license)

See file [LICENSE](https://github.com/juliangut/doctrine-base-repositories/blob/master/LICENSE) included with the source code for a copy of the license terms.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~119 days

Recently: every ~91 days

Total

6

Last Release

2948d ago

Major Versions

0.2 → 1.02017-10-07

PHP version history (2 changes)0.1PHP &gt;=5.6

0.2PHP ^7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/4c50421f1ab4148354dc2dd5dcaba168656b17ea913b310d112deb39a6f73ca1?d=identicon)[juliangut](/maintainers/juliangut)

---

Top Contributors

[![juliangut](https://avatars.githubusercontent.com/u/1104131?v=4)](https://github.com/juliangut "juliangut (106 commits)")

---

Tags

doctrinepaginationrepositorydoctrine2repository

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/juliangut-doctrine-base-repositories/health.svg)

```
[![Health](https://phpackages.com/badges/juliangut-doctrine-base-repositories/health.svg)](https://phpackages.com/packages/juliangut-doctrine-base-repositories)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.5k5.9M724](/packages/sylius-sylius)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k13](/packages/tempest-framework)[rector/rector-src

Instant Upgrade and Automated Refactoring of any PHP code

136406.3k14](/packages/rector-rector-src)[api-platform/metadata

API Resource-oriented metadata attributes and factories

275.0M210](/packages/api-platform-metadata)[2lenet/crudit-bundle

The easy like Crud'it Bundle.

1616.4k12](/packages/2lenet-crudit-bundle)[kassko/data-mapper

A mapper which gives a lot of features to representate some raw data like objects

1338.9k1](/packages/kassko-data-mapper)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
