GithubHelp home page GithubHelp logo

doctrine-scopes's Introduction

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.

$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):

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.