PHP code example of neutron / mongo-odm-silex-provider

1. Go to this page and download the library: Download neutron/mongo-odm-silex-provider library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

neutron / mongo-odm-silex-provider example snippets


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
                                                      }),
));

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

// 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();

  // ...
});


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

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

  // ...
});