matrix98/laravel_doctrine_odm

Doctrine ODM 的 Laravel 封装器

1.4.3 2021-06-08 16:01 UTC

This package is auto-updated.

Last update: 2024-08-29 05:37:27 UTC


README

此库为 Laravel 提供了 Doctrine ODM 封装器。

安装

建议通过 composer 进行安装

composer require "matrix98/laravel_doctrine_odm:~1.0.0"
  1. 在 'config/database.php' 文件中的 'connection' 数组索引子项中添加 MongoDB 配置参数
'mongodb'   => [
            'driver'        => 'mongodb',
            'dsn'           => 'mongodb://'.env('DB_HOST'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'port' => env('DB_PORT', '27017'),
            'database'      => env('DB_DATABASE'), // Default DB to perform queries against (not authenticate against)
            'retryConnect'  => 2, // Number of connection retry attempts before failing (doctrine feature)
            'retryQuery'    => 1, // Number of query retry attempts before failing (doctrine feature)
            'options'       => [
                // mapped to MongoClient $options
                'connectTimeoutMS'  => 1000, // Connection attempt timeout (milliseconds)
                'wTimeoutMS'        => 2500, // DB write attempt timeout (milliseconds)
                'socketTimeoutMS'   => 10000, // Client side socket timeout (milliseconds)
                'w'                 => 'majority', // Default write concern (normally w=1)
                'readPreference'    => 'primaryPreferred', // Default read preference
            ],
            'driverOptions' => [ // mapped to MongoClient $driverOptions (e.g. for SSL stream context)

            ]
        ],
  1. 发布 Laravel 服务提供者
php artisan vendor:publish --provider=LaravelDoctrineODM\ServiceProviders\IdeHelperServiceProvider

使用

使用依赖注入获取 DocumentManager 实例的示例

    use Doctrine\ODM\MongoDB\DocumentManager;

    class TestRepository {
        protected DocumentManager $dm;
    
        public function __construct(DocumentManager $dm)
        {
            $this->dm = $dm;
        }
    }

使用 DocumentManager 门面的示例

use LaravelDoctrineODM\Facades\DocumentManager;

$sm = DocumentManager::getSchemaManager();