ronanchilvers/silex-spot2-provider

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Provider for the Spot2 orm (vlucas/spot2)

1.0 2016-01-12 21:24 UTC

This package is auto-updated.

Last update: 2021-06-08 08:36:50 UTC


README

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