rework-devs/mongolid-laravel

此包已被弃用且不再维护。未建议替代包。

为 Laravel 提供简单、强大且超快的 MongoDB ODM。

v3.0.0 2022-07-15 14:20 UTC

README

Build Status Coverage Status Latest Stable Version Monthly Downloads Latest Unstable Version License

MongoLid

MongoLid (Laravel 包)

简介

MongoLid ODM (对象文档映射器) 为与 MongoDB 交互提供了美观、简单的实现。每个数据库集合都可以有一个对应的“模型”,用于与该集合交互。

注意:ODM 实现位于 (非 Laravel) mongolid 仓库

安装

使用 composer require 安装(如果需要,请使用上述标签之一)。

composer require leroy-merlin-br/mongolid-laravel

注意: Mongolid Laravel 2.0 仅支持 Laravel 5.4+。对于旧版本,请使用以下标签:

  • Laravel 4.2 "leroy-merlin-br/mongolid-laravel": "^0.7"
  • Laravel 5.1 "leroy-merlin-br/mongolid-laravel": "2.0.0-beta4"
  • Laravel 5.2 "leroy-merlin-br/mongolid-laravel": "2.0.0-beta6"
  • Laravel 5.3 "leroy-merlin-br/mongolid-laravel": "2.0.0-beta6"

使用 beta 标签时,请确保将最小稳定性设置为 devcomposer config minimum-stability dev)。

注意: 如果您正在使用 Laravel 5.5,则无需以下步骤添加提供者和别名。MongoLid 支持 Laravel 的新 包发现

在您的 config/app.php 中,将 'MongolidLaravel\MongolidServiceProvider' 添加到 $providers 数组的末尾

'providers' => [
    Illuminate\Translation\TranslationServiceProvider::class,
    Illuminate\Validation\ValidationServiceProvider::class,
    Illuminate\View\ViewServiceProvider::class,
    ...
    MongolidLaravel\MongolidServiceProvider::class,
],

(可选) 在 config/app.php 的末尾添加 'MongoLid' => 'MongolidLaravel\MongoLidModel'$aliases 数组

'aliases' => [
    'App'         => Illuminate\Support\Facades\App::class,
    'Artisan'     => Illuminate\Support\Facades\Artisan::class,
    ...
    'MongoLid'    => MongolidLaravel\MongoLidModel::class,
],

最后,请确保在 config/database.php 中配置数据库连接

将以下设置粘贴到您的 config/database.php 的末尾,在最后一个 ]; 之前

注意: 它必须在 connections 数组外。

/*
|--------------------------------------------------------------------------
| MongoDB Databases
|--------------------------------------------------------------------------
|
| MongoDB is a document database with the scalability and flexibility
| that you want with the querying and indexing that you need.
| Mongolid Laravel use this config to starting querying right now.
|
*/

'mongodb' => [
    'default' => [
        'host'     => env('DB_HOST', '127.0.0.1'),
        'port'     => env('DB_PORT_NUMBER', 27017),
        'database' => env('DB_DATABASE', 'my_database'),
        'username' => env('DB_USERNAME', null),
        'password' => env('DB_PASSWORD', null),
    ],
],

对于具有自动故障转移的集群,您需要设置包含所有 nodesreplica_set 名称的 cluster 键。

'mongodb' => [
    'default' => [
        'cluster' => [
            'replica_set' => env('DB_REPLICA_SET', null),
            'nodes' => [
                'primary' => [
                    'host' => env('DB_HOST_A', 'host-a'),
                    'port' => env('DB_PORT_A', 27017),
                ],
                'secondary' => [
                    'host' => env('DB_HOST_B', 'host-b'),
                    'port' => env('DB_PORT_B', 27017),
                ],
            ],
        ],
        'database' => env('DB_DATABASE', 'mongolid'),
        'username' => env('DB_USERNAME', null),
        'password' => env('DB_PASSWORD', null),
    ],
],

您可以配置所需数量的节点,节点名称(例如 primarysecondary)是可选的。

注意: 如果您在 config/database.php 中未指定 mongodb 键,MongoLid 将自动尝试连接到 '127.0.0.1:27017' 并使用名为 'mongolid' 的数据库。

您可以可选地提供 connection_string 键来设置一个完全组装的连接字符串,该字符串将覆盖所有其他连接选项。有关连接字符串的更多信息,请参阅 MongoDB 文档

'mongodb' => [
    'default' => [
        'connection_string' => 'mongodb://host-a:27017,host-b:27917/mongolid?replicaSet=rs-ds123',
    ],
],

此外,您还可以向 MongoDB 客户端传递 optionsdriver_options。Mongolid 总是覆盖 driver_optionstypeMap 配置为 array,因为它使得与模型内部使用更加容易。可能的 optionsdriver_options 请参阅 MongoDB\Client 文档

基本用法

要开始使用,请创建一个 MongoLid 模型。模型通常位于 app/models 目录中,但您可以自由地将它们放在任何根据您的 composer.json 文件自动加载的地方。

定义 MongoLid 模型

<?php
namespace App;

use MongolidLaravel\MongolidModel;

class User extends MongolidModel
{

}

总的来说,就是这样!

有关模型、CRUD操作、关系等内容,请查阅Read the Docs: leroy-merlin-br.github.com/mongolid

Mongolid Docs

身份验证

MongoLid Laravel 包含一个 Laravel 认证提供者。要使用它,只需将 config/auth.php 文件中的 'driver' 提供者值更改为 mongolid,并确保在 model 中指定的类是一个实现 Authenticatable 接口的 MongoLid 模型。

    'providers' => [

        // ...

        'users' => [
            'driver' => 'mongolid',
            'model' => \App\User::class
        ],

        // ...

    ],

User 模型应实现 Authenticatable 接口。

<?php
namespace App;

use Illuminate\Contracts\Auth\Authenticatable;
use MongolidLaravel\MongolidModel;

class User extends MongolidModel implements Authenticatable
{
    /**
     * The database collection used by the model.
     *
     * @var string
     */
    protected $collection = 'users';

    /**
     * Get the name of the unique identifier for the user.
     *
     * @return string
     */
    public function getAuthIdentifierName()
    {
        return '_id';
    }

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->_id;
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->remember_token;
    }


    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string  $value
     */
    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'remember_token';
    }
}

现在,要登录用户到您的应用程序,您可以使用 auth()->attempt() 方法。您可以使用有关认证的任何方法

队列失败作业提供者

Mongolid Laravel 替换了 Laravel 队列失败作业提供者,使用集合而不是表。要配置提供者,请更新 queue.php 文件中的 failed 键,包括 collection 名称。

    'failed' => [
        'database' => 'mongodb',
        'collection' => 'failed_jobs',
    ],

注意: database 键是无关紧要的。

故障排除

"PHP 致命错误:在 ... 中未找到类 'MongoDB\Client'"

MongoDB\Client 类包含在MongoDB PHP 库中,并需要MongoDB PHP 驱动程序。以下是此驱动程序的安装指南。该驱动程序是用 C 语言编写的 PHP 扩展,由MongoDB维护。MongoLid 和大多数其他 MongoDB PHP 库都使用它来确保快速和可靠。

"在 CLI 环境中未找到类 'MongoDB\Client'..." 即使安装了 MongoDB 驱动程序也持续存在。

请确保 CLI 环境中使用的 php.ini 文件包含 MongoDB 扩展。在某些系统中,默认的 PHP 安装为 Web 和 CLI 环境使用不同的 .ini 文件。

在终端中运行 php --ini 以检查正在使用的 .ini

要在终端中检查 CLI 环境中的 PHP 是否正确导入驱动程序,请运行 php -i | grep mongo。您应该得到类似以下输出:

$ php -i | grep mongo
mongodb
mongodb support => enabled
...

"此包需要 php >=7.0,但您的 PHP 版本 (X.X.X) 不满足该要求。"

Mongolid Laravel 的新版(和改进版)2.0 需要 php7。如果您正在寻找旧的 PHP 5.x 版本或其他 Laravel 版本,请转到 v0.8 分支

许可证

MongoLid 和 MongoLid Laravel 是免费软件,根据MIT 许可协议分发。部分代码基于 Taylor Otwell 和laravel/framework的贡献者的工作,后者也是免费软件,根据MIT 许可协议分发。

更多信息

Mongolid 由Leroy Merlin Brazil团队自豪地构建。查看所有贡献者

如有任何问题,请随时联系我们。

如有任何问题,请在此处报告