astraller / yii2-mongodb-embedded

Yii2 扩展,用于处理 MongoDB 中的嵌入式文档。

安装: 2

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

1.1.0 2018-09-25 18:01 UTC

This package is auto-updated.

Last update: 2024-09-26 07:59:40 UTC


README

Yii2 扩展,用于处理 MongoDB 中的嵌入式文档。


此扩展需要 MongoDB PHP 扩展版本 1.0.0 或更高。
此扩展需要 MongoDB 服务器版本 3.0 或更高。

安装

安装此扩展的首选方式是通过 composer。
运行以下命令:

php composer.phar require --prefer-dist astraller/yii2-mongodb-embedded

或者

"astraller/yii2-mongodb-embedded": "^1.*"

将以下内容添加到您的 composer.json 文件的 require 部分:

使用方法

  • 您的模型必须继承自 astraller\mongodb\MongoModel 类。
  • 添加 embedAttributes 方法
    public function embedAttributes() {
        return [
            'conditions' => [
                'class' => DialogConditions::class,
                'type'  => 'many',
            ],
            'text'       => [
                'class' => Text::class,
                'type'  => 'one',
            ],
            'variants' => [
                'class' => Dialog::class,
                'type' => 'manyById'
            ]
        ];
    }
  • 您的嵌入式文档必须继承自 EmbeddedDocument 类。
<?php
namespace app\models;

use astraller\mongodb\MongoEmbedModel;

class Vector3 extends MongoEmbedModel
{

    public $x;
    public $y;
    public $z;

    public function rules()
    {
        return [
            [['x', 'y', 'z'], 'required'],
        ];
    }

    public function t(){
        return "[X: {$this->x}; Y: {$this->y}; Z: {$this->z}]";
    }

}