PHP code example of df / silex-doctrine-mongodb-odm-provider
1. Go to this page and download the library: Download df/silex-doctrine-mongodb-odm-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/ */
df / silex-doctrine-mongodb-odm-provider example snippets
// Default document manager.
$em = $app['mongodbodm.dm'];
use DF\DoctrineMongoDb\Provider\DoctrineMongoDbProvider;
use DF\DoctrineMongoDbOdm\Provider\DoctrineMongoDbOdmProvider;
$container = new \Pimple;
$container["mongodb.options"] = [
"server" => "mongodb://localhost:27017",
"options" => [
'username' => 'your-username',
'password' => 'your-password',
'db' => 'mongo_db_01'
]
];
$container["mongodbodm.proxies_dir"] = "/path/to/proxies";
$container["mongodbodm.hydrator_dir"] = "/path/to/hydrator";
$container["mongodbodm.dm.options"] = [
"database" => "test",
"mappings" => [
// Using actual filesystem paths
[
"type" => "annotation",
"namespace" => "Foo\Entities",
"path" => __DIR__."/src/Foo/Entities",
],
[
"type" => "xml",
"namespace" => "Bat\Entities",
"path" => __DIR__."/src/Bat/Resources/mappings",
],
// Using PSR-0 namespace embedded resources
// (
use DF\DoctrineMongoDb\Silex\Provider\DoctrineMongoDbProvider;
use DF\DoctrineMongoDbOdm\Silex\Provider\DoctrineMongoDbOdmProvider;
use Silex\Application;
use Silex\Provider\DoctrineServiceProvider;
$app = new Application;
$app->register(new DoctrineMongoDbProvider, [
"mongodb.options" => [
"server" => "mongodb://localhost:27017",
"options" => [
'username' => 'your-username',
'password' => 'your-password',
'db' => 'mongo_db_01'
],
],
]);
$app->register(new DoctrineMongoDbOdmProvider, array(
"mongodbodm.proxies_dir" => "/path/to/proxies",
"mongodbodm.hydrator_dir" => "/path/to/hydrator",
"mongodbodm.dm.options" => array(
"database" => "test",
"mappings" => array(
// Using actual filesystem paths
array(
"type" => "annotation",
"namespace" => "Foo\Entities",
"path" => __DIR__."/src/Foo/Entities",
),
array(
"type" => "xml",
"namespace" => "Bat\Entities",
"path" => __DIR__."/src/Bat/Resources/mappings",
),
// Using PSR-0 namespace embedded resources
// (
use DF\DoctrineMongoDb\Cilex\Provider\DoctrineMongoDbProvider;
use DF\DoctrineMongoDbOdm\Cilex\Provider\DoctrineMongoDbOdmProvider;
use Cilex\Application;
use Cilex\Provider\DoctrineServiceProvider;
$app = new Application('My Application');
$app->register(new DoctrineMongoDbProvider, array(
/** same as the Silex example **/
));
$app->register(new DoctrineMongoDbOdmProvider, array(
/** same as the Silex example **/
));
$app['mongodbodm.dms.default'] = 'sqlite';
$app['mongodbodm.dms.options'] = array(
'mongo1' => array(
'server' => 'mongodb://localhost:27017',
'options' => array(
'username' => 'root',
'password' => 'root',
'db' => 'admin'
)
),
'mongo2' => array(
'server' => 'mongodb://localhost:27018',
'options' => array(
'username' => 'root',
'password' => 'root',
'db' => 'admin'
)
)
);
$emMysql = $app['mongodbodm.dms']['mongo1'];
$emSqlite = $app['mongodbodm.dms']['mongo2'];
$emName = $app['mongodbodm.dm_name_from_param']('3rdparty.provider.dm');
$em = $app['mongodbodm.dms'][$emName];
$app['mongodbodm.dms.config'] = $app->share($app->extend('mongodbodm.dms.config', function ($config, $app) {
$mapping = $app['mongodbodm.generate_psr0_mapping'](array(
'Foo\Resources\mappings' => 'Foo\Entities',
'Bar\Resources\mappings' => 'Bar\Entities',
));
$chain = $app['mongodbodm.mapping_driver_chain.locator']();
foreach ($mapping as $directory => $namespace) {
$driver = new XmlDriver($directory);
$chain->addDriver($driver, $namespace);
}
return $config;
}));
$loader = on\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));