Code Monkey home page Code Monkey logo

spraypersistencebundle's Introduction

Spray\PersistenceBundle

A Symfony2 bundle that enhances Doctrine2 repository functionality.

Build Status

The problem

Doctrine2 provides a nice api that lets you query for your entities through the Repository. However the implemented Repository pattern is not very DRY. If you have more complicated queries to execute the suggested solution is to override the default Repository class and add methods as follows:

class MyRepository extends Repository
{
    public function findPublished()
    {
        // Perform query
    }
}

When your application grows larger you might need to find published entities before a specific date:

class MyRepository extends Repository
{
    public function findPublished()
    {
        // Perform query
    }

    public function findPublishedBeforeDate(DateTime $date)
    {
        // Same logic as findPublished()
        // Perform before date query
    }
}

Now you need a published entity, before a specific date, within a radius of your location.

At this moment you'll create more and more duplicate code. That's where the FilterableEntityRepository comes in. The above logic can be rewritten as:

class EntityPublished implements EntityFilterInterface
{
    public function filter(QueryBuilder $qb)
    {
        // Perform query
    }
}

class EntityBeforeDate extends AbstractBeforeFilter
{
    protected $propertyName = 'foo';
}

class EntityWithinRadius implements EntityFilterInterface
{
    public function filter(QueryBuilder $qb)
    {
        // Perform query
    }
}

$repository = new FilterableEntityRepository();
$repository->filter(new EntityPublished());
$repository->filter(new EntityBeforeDate());
$repository->filter(new EntityWithinRadius());

Prioritized filters

You may want a filter to be prioritized. To do so you must implement the PrioritizedFilterInterface:

use Doctrine\ORM\QueryBuilder;
use Spray\PersistenceBundle\EntityFilter\PrioritizedFilterInterface;

class PrioritizedFilter implements PrioritizedFilterInterface
{
    public function filter(QueryBuilder $qb)
    {
        // Do stuff
    }

    public function getName()
    {
        return 'prioritized_filter';
    }

    public function getPriority()
    {
        return 100;
    }
}

$repository->filter(new PrioritizedFilter()); // Added at priority level 100

Conflicting filters

If you have two filters that can conflict with another (for instance if they create a where statement on the same column) you may implement the ConflictingFilterInterface:

use Doctrine\ORM\QueryBuilder;
use Spray\PersistenceBundle\EntityFilter\ConflictingFilterInterface;

class ConflictingFilter implements ConflictingFilterInterface
{
    public function filter(QueryBuilder $qb)
    {
        // Do stuff
    }

    public function getName()
    {
        return 'conflicting_filter';
    }

    public function getConflictingFilters()
    {
        return array('another_filter');
    }
}

spraypersistencebundle's People

Contributors

jurjean avatar

Watchers

Bart van den Burg avatar James Cloos avatar

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.