Code Monkey home page Code Monkey logo

mongodb-odm-silex-provider's Introduction

MongoDB ODM Silex Service Provider

Build Status

This Silex service provider provides Doctrine MongoDB ODM service in your Silex Application.

This provider has been developed by Justin Hileman and Florian Klein

It seems that both of these projects are not maintained anymore, so here comes the maintained version, compatible with Doctrine MongoDB ODM >= 1.0.0-beta4.

Enjoy

Usage

Composer

Add in your composer.json the require entry for this library.

{
    "require": {
        "neutron/mongo-odm-silex-provider": "*"
    }
}

and run composer install (or update) to download all files, resolve dependencies and update the autoloading.php file.

Registering

You can register MongoDB ODM Provider using:

use Neutron\Silex\Provider\MongoDBODMServiceProvider;

// ...

$app->register(new MongoDBODMServiceProvider(), array(
    'doctrine.odm.mongodb.connection_options' => array(
        'database' => 'MONGODB_DB',

        // connection string:
        // mongodb://[username:password@]host1[:port1][,host2[:port2:],...]/db
        'host'     => 'MONGODB_SERVER',

        // connection options as described here:
        // http://www.php.net/manual/en/mongoclient.construct.php
        'options'  => array('fsync' => false)
    ),
    'doctrine.odm.mongodb.documents'               => array(),
    'doctrine.odm.mongodb.proxies_dir'             => 'cache/doctrine/odm/mongodb/Proxy',
    'doctrine.odm.mongodb.proxies_namespace'       => 'DoctrineMongoDBProxy',
    'doctrine.odm.mongodb.auto_generate_proxies'   => true,
    'doctrine.odm.mongodb.hydrators_dir'           => 'cache/doctrine/odm/mongodb/Hydrator',
    'doctrine.odm.mongodb.hydrators_namespace'     => 'DoctrineMongoDBHydrator',
    'doctrine.odm.mongodb.auto_generate_hydrators' => true,
    'doctrine.odm.mongodb.metadata_cache'          => new \Doctrine\Common\Cache\ArrayCache(),
    'doctrine.odm.mongodb.logger_callable'         => $app->protect(function($query) {
                                                          // log your query
                                                      }),
));

Configuration

The configuration params listed above can be omitted if you use the default values.

  • doctrine.odm.mongodb.documents: document management registration. This option will be explaned in the next praragraph.
  • doctrine.odm.mongodb.proxies_dir: directory for proxy classes generated by Doctrine.
  • doctrine.odm.mongodb.proxies_namespace: namespace for proxy classes generated by Doctrine.
  • doctrine.odm.mongodb.hydrators_dir: directory for hydrators classes generated by Doctrine.
  • doctrine.odm.mongodb.hydrators_namespace: namespace for hydrators classes generated by Doctrine.
  • doctrine.odm.mongodb.metadata_cache: instace of class to use as caching system. Maybe you can change to \Doctrine\Common\Cache\ApcCache() to use APC caching system.
  • doctrine.odm.mongodb.logger_callable: Callable used for logging, must wrap functions using $app->protect() to prevent from them being interpreted by Pimple

Document registration

You need to register the differents documents namespace to make possible use it with Doctrine ODM, eg:

$app->register(new MongoDBODMServiceProvider(), array(
    // ...
    'doctrine.odm.mongodb.documents' => array(
        0 => array(
            'type' => 'annotation',
            'path' => array(
                'src/Acme/Entities',
            ),
            'namespace' => 'Acme/Entities',
            'alias'     => 'docs',
        ),
    ),
    // ...
));

you can add multiple folders/namespaces.

Usage

Add information in MongoDB:

// Define routing
$app->post('/demo/add', function () use ($app) {
  // ...
  $demo = new \Acme\Document\Demo();
  $demo->setTitle('Test');
  $demo->setBody('This is a demo document.');

  $app['doctrine.odm.mongodb.dm']->persist($demo);
  $app['doctrine.odm.mongodb.dm']->flush();

  // ...
});

Retrieve information from MongoDB:

$app->get('/demo/list', function () use ($app) {
  // ...
  $demos = $app['doctrine.odm.mongodb.dm']
    ->getRepository('Acme\\Document\\Demo')
    ->findAll();

  // ...
});

if you have defined an alias, you can use it:

$app['doctrine.odm.mongodb.dm']->getRepository('docs:Demo');

Creating console

Sometime can be usefull have console to manage your Doctrine ODM. You can configure your console to use it with:

use Symfony\Component\Console\Application;
// .. other class to import
use Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper;

$console = new Application('Acme Application', '1.0');

// Other commands

// This make possibile to re-use default Doctrine console command
$dm = new DocumentManagerHelper($app['doctrine.odm.mongodb.dm']);
$console->getHelperSet()->set($dm, 'dm');

// Add Doctrine ODM commands
$console->addCommands(array(
    new Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateDocumentsCommand(),
    new Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateHydratorsCommand(),
    new Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateProxiesCommand(),
    new Doctrine\ODM\MongoDB\Tools\Console\Command\GenerateRepositoriesCommand(),
    new Doctrine\ODM\MongoDB\Tools\Console\Command\QueryCommand(),
    new Doctrine\ODM\MongoDB\Tools\Console\Command\ClearCache\MetadataCommand(),
));

License

Released under the MIT license

mongodb-odm-silex-provider's People

Contributors

romainneutron avatar myurasov avatar mavimo avatar cranberyxl avatar mdrollette avatar lucianodelucchi avatar

Stargazers

Vladimir Tkachev avatar

Watchers

Edward Prendergast 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.