Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

ronanchilvers/silex-spot2-provider

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Silex Provider for the Spot2 ORM

SensioLabsInsight

Spot2 is a simple ORM for Silex projects based on the data mapper pattern. You can read more about it on the github page or the documentation site.

This project comprises a service provider and utility for hooking Spot2 up to your Silex app.

Installation

The easiest mechanism is via composer. :

composer require ronanchilvers/silex-spot2-provider ^1.0

Silex2 is supported in the master branch although there isn't yet a release. If you want to use the Silex2 version you can do:

composer require ronanchilvers/silex-spot2-provider dev-master

Usage

To register the service provider you can do something like this:

$app->register(new \Ronanchilvers\Silex\Provider\Spot2ServiceProvider(), [
    'spot2.connections' => [
        'default' => 'sqlite://path/to/my_database.sqlite'
    ]
]);

$app->get('/things', function() use ($app) {
    $mapper = $app['spot2.locator']->mapper('Entity\Thing');

    $this->render('things.twig', [
        'things' => $mapper->all()
    ]);
});

Utility trait

If you want to use the utility trait, just include it in your subclassed application.

class Application extends Silex\Application
{
    use \Ronanchilvers\Silex\Application\Spot2Trait;
}

You then get the following convenience methods.

mapper()

This method is a shortcut for getting a mapper out for a particular entity.

$app->get('/things', function() use ($app) {
    $mapper = $this->mapper('Entity\Thing');

    return $this->render('things.twig', [
        'things' => $mapper->all()
    ]);
});

spot2()

This method is a shortcut for getting the spot2 locator object. Most of the time you won't use this method (you'll probably use mapper() instead). It exists just to allow easy access to the locator if required.

$app->get('/things', function() use ($app) {
    $mapper = $this->spot2()->mapper('Entity\Thing');

    return $this->render('things.twig', [
        'things' => $mapper->all()
    ]);
});

Services Exposed

The Spot2ServiceProvider exposes the following services.

  • spot2.connections - an array of connection DSNs or DBAL style configuration arrays
  • spot2.connections.default - the name of the default connection to use. Spot2 will take the first defined one as the default if you don't set one explicitly.
  • spot2.config- the Spot2 config object
  • spot2.locator - the Spot2 locator object