PHPackages                             randomstate/doctrine-scopes - 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. randomstate/doctrine-scopes

ActiveLibrary

randomstate/doctrine-scopes
===========================

Global query scopes for Doctrine2

v0.1.4(2y ago)178MITPHP

Since Mar 11Pushed 2y ago2 watchersCompare

[ Source](https://github.com/randomstate/doctrine-scopes)[ Packagist](https://packagist.org/packages/randomstate/doctrine-scopes)[ RSS](/packages/randomstate-doctrine-scopes/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (5)Versions (6)Used By (0)

Doctrine Scopes
===============

[](#doctrine-scopes)

One of the most important tasks in modern apps (particularly multi-tenant SaaS apps) is to ensure data isolation between your customers. The 'best practice' for this is to filter all your queries at the global level.

Laravel Eloquent provides it. Doctrine2 didn't.

This package fixes that 👌

**This gives you the full power of the query builder for scoping your queries, whereas the built-in Filters doesn't allow joins and expects you to write the SQL yourself.**

Getting Started
---------------

[](#getting-started)

`composer require randomstate/doctrine-scopes`

Usage
-----

[](#usage)

Where you would normally create your entity manager, wrap it in a decoratable one and inject a scoped query builder factory closure.

```
$scopes = new ScopeCollection();
$scope->add('myscope', new MyScope());
$scope->enable('myscope');

// Replace query builders with scopable ones
$em = new DecoratableEntityManager(new EntityManager(...));
$em->setQueryBuilderFactory(function() use($em, $scopes) {
    return new ScopableQueryBuilder($em, $scopes);
});

// Wrap repositories so that they are scoped
$em->extendRepositoryFactory(function(EntityRepository $repository) use($em) {
    return new ScopedEntityRepository($repository, $em);
})

$em->find(MyClass::class, 1); // this query is now scoped by whatever you have in MyScope@apply 🎉
```

### Laravel

[](#laravel)

For laravel users, this is easier:

Add `RandomState\DoctrineScopes\DoctrineScopesServiceProvider::class` to your providers list in your `config/app.php` file.

In the boot method of a service provider of your choice (e.g. `AppServiceProvider`):

```
public function boot() {
    $this->app->extend(RandomState\DoctrineScopes\ScopeCollection::class, function($scopes) {
        $scopes->add('myscope', new MyScope);
        $scopes->enable('myscope');

        return $scopes;
    });
}
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 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 ~384 days

Total

5

Last Release

1082d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1204796510f0f912ac6f85359e20efbc78e573c00c742a3a53f509f3abdc7f9f?d=identicon)[CImrie](/maintainers/CImrie)

---

Top Contributors

[![byfoz](https://avatars.githubusercontent.com/u/3436576?v=4)](https://github.com/byfoz "byfoz (2 commits)")

---

Tags

doctrine2filtersmulti-tenantquerybuilderscopes

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/randomstate-doctrine-scopes/health.svg)

```
[![Health](https://phpackages.com/badges/randomstate-doctrine-scopes/health.svg)](https://phpackages.com/packages/randomstate-doctrine-scopes)
```

###  Alternatives

[sonata-project/entity-audit-bundle

Audit for Doctrine Entities

644989.8k1](/packages/sonata-project-entity-audit-bundle)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)[api-platform/doctrine-orm

Doctrine ORM bridge

243.1M39](/packages/api-platform-doctrine-orm)[neos/flow-development-collection

Flow packages in a joined repository for pull requests.

144179.3k3](/packages/neos-flow-development-collection)[pixelfederation/doctrine-resettable-em-bundle

Symfony bundle for decorating default entity managers using a resettable decorator.

20113.5k](/packages/pixelfederation-doctrine-resettable-em-bundle)[leapt/core-bundle

Symfony LeaptCoreBundle

2529.1k4](/packages/leapt-core-bundle)

PHPackages © 2026

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