https://github.com/randomstate/doctrine-scopes
Global query builder scopes for Doctrine2
https://github.com/randomstate/doctrine-scopes
doctrine2 filters multi-tenant querybuilder scopes
Last synced: 6 months ago
JSON representation
Global query builder scopes for Doctrine2
- Host: GitHub
- URL: https://github.com/randomstate/doctrine-scopes
- Owner: randomstate
- Created: 2019-03-10T23:47:44.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-05-27T21:51:27.000Z (over 2 years ago)
- Last Synced: 2025-02-05T16:22:42.118Z (8 months ago)
- Topics: doctrine2, filters, multi-tenant, querybuilder, scopes
- Language: PHP
- Size: 36.1 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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
`composer require randomstate/doctrine-scopes`
## Usage
Where you would normally create your entity manager, wrap it in a decoratable one and inject a scoped query builder factory closure.
```php
$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
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`):
```phppublic function boot() {
$this->app->extend(RandomState\DoctrineScopes\ScopeCollection::class, function($scopes) {
$scopes->add('myscope', new MyScope);
$scopes->enable('myscope');
return $scopes;
});
}```