brunohulk/yii2-mongodb-doctrine

Doctrine MongoDB ODM 扩展用于 Yii2

1.1.0 2017-10-11 19:03 UTC

This package is not auto-updated.

Last update: 2024-09-19 05:03:24 UTC


README

Latest Stable Version Downloads License

关于

此组件的创建目的是为了让我们能够通过 MongoDB ODM 版本在 Yii2 中使用 Doctrine 的所有优势。虽然也有一些使用 MongoDB 和 Yii 的 active record 模式的解决方案,但丰富的 Doctrine 文档和其处理嵌入式文档的更好方式使其脱颖而出。

安装

使用 Composer 安装库

composer require brunohulk/yii2-mongodb-doctrine

然后,为了激活组件,您需要在 web.php 文件中添加以下条目,用您自定义的数据替换默认的数据库参数

'doctrineOdm' => [
    'class' => 'brunohulk\Yii2MongodbOdm\DoctrineODM',
    'dsn' => 'mongodb://mongodb:27017', #DSN string connection
    'dbname' => 'database_name'  #Database name,
    'documentsDir' => __DIR__ . '/../../documents/', # Directory which stores your mapped collections
    'runtimeDir' =>  __DIR__ . '/../../runtime/' # The Yii2 runtime dir or other directory to store the Doctrine extra files
]

使用方法

要开始使用文档管理器,您只需在任何您希望的地方调用以下方法,例如控制器

class User extends Controller
{
    private $documentManager;

    public function init()
    {
        $this->documentManager = Yii::$app->doctrineOdm->getDocumentManager();
    }

    public function actionCreate()
    {
        $user = new User;
        $user->name = "Bruno";

        $this->documentManager->persist($user);
        $this->documentManager->flush();

    }

对于最后一步,您需要在您的 Yii 项目的 common 目录中创建一个 documents 文件夹,所有映射的文档都必须放在那里,以下示例与之前的文档块相关。

/**
 * @ODM\Document(collection="user")
 */
class User
{
    /**
     * @ODM\Id
     */
    public $id;

    /**
     * @ODM\Field(name="name", type="string")
     */
    public $name;

}

特别感谢 David Rocha