devture/silex-provider-doctrine-mongodb

Silex 框架的 MongoDB 集成提供商,通过 Doctrine 实现

2.0 2021-12-07 06:52 UTC

This package is not auto-updated.

Last update: 2024-09-10 19:44:20 UTC


README

doctrine/mongodb 提供的 Silex 微框架的提供商。

用法

注册提供商会在指定的 命名空间 下创建一些服务。如果需要多个连接,可以多次注册提供商。

基本用法

<?php
// Register services under the 'mongodb' namespace, with no custom configuration (empty array)
$app->register(new \Devture\SilexProvider\DoctrineMongoDB\ServicesProvider('mongodb', []));

// Get a reference to a database on that server connection
$app['db'] = function ($app) {
	return $app['mongodb.connection']->selectDatabase('database_name');
};

高级用法

<?php
// See the docs for \MongoClient (https://php.ac.cn/MongoClient) for connection string format and options
$configuration = [
	'server' => 'mongodb://example.com:27017',
	'options' => [
		'connect' => true,
		'connectTimeoutMS' => 200,
	],
];

$app->register(new \Devture\SilexProvider\DoctrineMongoDB\ServicesProvider('mongodb', $configuration));

$app['db_main'] = function ($app) {
	return $app['mongodb.connection']->selectDatabase('database_name');
};

$app['db_other'] = function ($app) {
	return $app['mongodb.connection']->selectDatabase('another_database_name');
};