【问题标题】:Connecting slim php to mongodb doctrine将 slim php 连接到 mongodb 学说
【发布时间】:2021-06-10 09:56:58
【问题描述】:

我将这个苗条的 php 框架用于 mvc 结构。它也有教义联系https://github.com/semhoun/slim-skeleton-mvc

我想连接到我的 mongodb 服务器,这是学说 mongodb 包https://www.doctrine-project.org/projects/mongodb-odm.html

我已经安装并设置了我的bootstrap.php,就像文档在设置步骤https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/2.2/reference/introduction.html#setup中所说的那样

我的 slim 框架中的设置文件是这样的

<?php
declare(strict_types=1);

use DI\ContainerBuilder;
use Monolog\Logger;

return function (ContainerBuilder $containerBuilder) {
    $rootPath = realpath(__DIR__ . '/..');

    // Global Settings Object
    $containerBuilder->addDefinitions([
        'settings' => [
            // Base path
            'base_path' => '',
        
            // Is debug mode
            'debug' => (getenv('APPLICATION_ENV') != 'production'),

            // 'Temprorary directory
            'temporary_path' => $rootPath . '/var/tmp',

            // Route cache
            'route_cache' =>$rootPath . '/var/cache/routes',

            // View settings
            'view' => [
                'template_path' =>$rootPath . '/tmpl',
                'twig' => [
                    'cache' =>$rootPath . '/var/cache/twig',
                    'debug' => (getenv('APPLICATION_ENV') != 'production'),
                    'auto_reload' => true,
                ],
            ],

            // doctrine settings
            'doctrine' => [
                'meta' => [
                    'entity_path' => [ $rootPath . '/src/Entity' ],
                    'auto_generate_proxies' => true,
                    'proxy_dir' => $rootPath . '/var/cache/proxies',
                    'cache' => null,
                ],
                'connection' => [
                    'driver' => 'pdo_sqlite',
                    'path' => $rootPath . '/var/blog.sqlite'
                ]
            ],

            // monolog settings
            'logger' => [
                'name' => 'app',
                'path' =>  getenv('docker') ? 'php://stdout' : $rootPath . '/var/log/app.log',
                'level' => (getenv('APPLICATION_ENV') != 'production') ? Logger::DEBUG : Logger::INFO,
            ]
        ],
    ]);

    if (getenv('APPLICATION_ENV') == 'production') { // Should be set to true in production
        $containerBuilder->enableCompilation($rootPath . '/var/cache');
    }
};

如何连接到我的 mongodb 服务器并在我的控制器中使用它?

【问题讨论】:

    标签: php mongodb doctrine-orm slim php-mongodb


    【解决方案1】:

    确保ext-mongodb 已安装。

    extension=mongodb
    

    安装 Doctrine MongoDB ODM 库:

    composer require doctrine/mongodb-odm
    

    DocumentManager::class添加一个DI容器定义:

    <?php
    
    use Doctrine\ODM\MongoDB\Configuration;
    use Doctrine\ODM\MongoDB\DocumentManager;
    use MongoDB\Client;
    use Psr\Container\ContainerInterface;
    
    return [
        // ...
    
        DocumentManager::class => function (ContainerInterface $container) {
            $settings = $container->get('settings')['mongodb'];
    
            // URI: mongodb://127.0.0.1
            $client = new Client($settings['uri']);
            $config = new Configuration();
            // ...
    
            return DocumentManager::create($client, $config);
        },
    ];
    
    

    然后使用依赖注入并在你的 Repository 类构造函数中声明DocumentManager

    使用示例:

    $address = new Address();
    $address->setAddress('555 Doctrine Rd.');
    $address->setCity('Nashville');
    $address->setState('TN');
    $address->setZipcode('37209');
    
    $this->dm->persist($address);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多