PHP code example of juliangut / slim-doctrine
1. Go to this page and download the library: Download juliangut/slim-doctrine 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/ */
juliangut / slim-doctrine example snippets
[
ManagerBuilder::DEFAULT_RELATIONAL_MANAGER_KEY => [
'manager1_name' => <relational_manager_builder_configuration>,
'manager2_name' => <relational_manager_builder_configuration>,
...
],
ManagerBuilder::DEFAULT_MONGODB_MANAGER_KEY => [
'manager3_name' => <mongodb_manager_builder_configuration>,
'manager4_name' => <mongodb_manager_builder_configuration>,
...
],
ManagerBuilder::DEFAULT_COUCHDB_MANAGER_KEY => [
'manager5_name' => <couchdb_manager_builder_configuration>,
'manager6_name' => <couchdb_manager_builder_configuration>,
...
],
]
$options = [
ManagerBuilder::RELATIONAL_MANAGER_KEY => 'entity_manager',
ManagerBuilder::MONGODB_MANAGER_KEY => 'mongodb_document_manager',
ManagerBuilder::COUCHDB_MANAGER_KEY => 'couchdb_document_manager',
ManagerBuilder::RELATIONAL_MANAGER_NAME => 'entityManager',
ManagerBuilder::MONGODB_MANAGER_NAME => 'mongoDocumentManager',
ManagerBuilder::COUCHDB_MANAGER_NAME => 'couchDocumentManager',
];
$managerBuilder = new ManagerBuilder($options);
use Jgut\Slim\Doctrine\ManagerBuilder;
use Slim\App;
// Loaded from a file
$settings = [
'my_custom_key' => [
'annotation_autoloaders' => ['class_exists'],
'connection' => [
'driver' => 'pdo_sqlite',
'memory' => true,
],
'metadata_mapping' => [
[
'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION,
'path' => 'path/to/annotation/mappings',
],
],
],
];
$managerBuilder = new ManagerBuilder([ManagerBuilder::RELATIONAL_MANAGER_KEY => 'my_custom_key']);
$managerBuilder->loadSettings($settings);
// Create Slim app and fetch DI Container
$app = new App();
$container = $app->getContainer();
// Register every manager in the container
foreach ($managerBuilder->getManagers() as $name => $manager) {
$container[$name] = $manager;
}
// Use managers
$app->get('/', function () {
$this->entityManager->persist(new \Entity);
$this->entityManager->flush();
});
use Jgut\Slim\Doctrine\ManagerBuilder;
use Interop\Container\ContainerInterface;
use Slim\App;
// Probably loaded from a file...
$settings = [
'settings.doctrineManagers' => [
ManagerBuilder::DEFAULT_RELATIONAL_MANAGER_KEY => [
'mainDocumentManager' => [
'connection' => [
'server' => 'mongodb://localhost:27017',
],
'metadata_mapping' => [
[
'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION,
'path' => 'path/to/annotation/mappings',
],
],
],
'secondaryDocumentManager' => [
'annotation_autoloaders' => ['class_exists'],
'connection' => [
'server' => 'mongodb://localhost:27017',
],
'metadata_mapping' => [
[
'type' => ManagerBuilder::METADATA_MAPPING_ANNOTATION,
'path' => 'path/to/annotation/mappings',
],
],
],
],
],
];
// Create Slim app and fetch DI Container
$app = new App($settings);
$container = $app->getContainer();
// Register manager builder fetching settings from container
$container['manager_builder'] => function (ContainerInterface $container) {
return (new ManagerBuilder())->loadSettings($container->get('settings.doctrineManagers'));
};
// Register managers by pulling them from the builder
$container['mainDocumentManager'] => function (ContainerInterface $container) {
return $container->get('manager_builder')->getManager('mainDocumentManager');
};
$container['secondaryDocumentManager'] => function (ContainerInterface $container) {
return $container->get('manager_builder')->getManager('secondaryDocumentManager');
};
// Use managers
$app->get('/', function () {
$this->mainDocumentManager->persist(new \Document);
$this->mainDocumentManager->flush();
});
use Jgut\Slim\Doctrine\ManagerBuilder;
$settings = gs($settings);
return $managerBuilder->getCLIApplication();