eman-development-design/mongodb-helpers

帮助对象,使编写MongoDB应用程序更加容易。

1.0.5 2020-12-22 03:54 UTC

This package is auto-updated.

Last update: 2024-09-12 03:36:00 UTC


README

帮助对象,使编写MongoDB应用程序更加容易。

帮助工具

排序帮助工具

您可以通过扩展DataSorting轻松添加排序。

class UserSorting extends DataSorting
{
    public const Email = 'Email';

    public const FirstName = 'FirstName';

    public const LastName = 'LastName';
}

您可以使用如下方式使用它

UserSorting::AddToSortList(UserSorting::FirstName, UserSorting::ASC);

然后您可以通过这种方式编译排序列表

$sort = UserSorting::GetSortList();

这将允许您向查询添加任意数量的排序规则。

模型帮助工具

MongoModelInterface是一个接口帮助工具,将提供一些处理模型数据的方法。

use MongoDB\BSON\Serializable;
use MongoDB\Model\BSONDocument;
use Edd\MongoDbHelpers\MongoModelInterface

class User implements Serializable, MongoModelInterface
{
    public $userGuid;
    public $email;
    public $firstName;
    public $lastName;
    
    public function bsonSerialize() : array
    {
        return [
            'UserUuid' => $this->userUuid,
            'Email' => $this->email,
            'FirstName' => $this->firstName,
            'LastName' => $this->lastName
        ];
    }

    public function map(BSONDocument $document)
    {
        $this->userUuid = $document['UserUuid'];
        $this->email = $document['Email'];
        $this->firstName = $document['FirstName'];
        $this->lastName = $document['LastName'];
    }

    public function toArray() : array
    {
        return get_object_vars($this);
    }
}

map帮助您将BSON文档结果映射到模型属性。

toArray用于将数据呈现为数组。

仓库帮助工具

MongoRepository使得设置基本的仓库风格架构变得简单,构造函数将创建一个MongoDB\Client实例。

SetCollectionGetCollection设置并提供一个MongoDB\Collection实例。