roaresearch/yii2-rmdb

用于创建 RMDB 模型和迁移的 Yii 2 库

3.0.0 2022-10-12 15:14 UTC

This package is auto-updated.

Last update: 2024-09-24 02:37:14 UTC


README

包含迁移和模型的库,可轻松创建 RMDB 表和模型。

安装

您可以通过运行以下命令使用 composer 安装库 roaresearch/yii2-rmdb

composer require roaresearch/yii2-rmdb

或者编辑 composer.json 文件

require: {
    "roareasearch/yii2-rmdb": "*",
}

使用方法

创建迁移

每种 RMDB 表类型都有 3 个迁移类。

roaresearch\yii2\rmdb\migrations\CreatePivot

使用属性 $createdByColumn$createdAtColumn 和方法 createdByDefinition()createdAtDefinition() 来存储创建记录的用户和日期时间。

use roaresearch\yii2\rmdb\migrations\CreatePivot;

class m170101_000001_product_sale extends CreatePivot
{
    public $createdByColumn = 'creation_user';
    public $createdAtColumn = 'creation_date';

    public function getTableName()
    {
        return 'product_sale';
    }

    public function columns()
    {
        return [
            'product_id' => ...,
            'sale_id' => ...,
        ];
    }

    public function compositePrimaryKeys()
    {
        return ['product_id', 'sale_id'];
    }
}

roaresearch\yii2\rmdb\migrations\CreateEntity

扩展了前面的类,添加了属性 $updatedByColumn$updatedAtColumn 和方法 updatedByDefinition()updatedAtDefinition() 来存储更新记录的用户和日期时间。

use roaresearch\yii2\rmdb\migrations\CreateEntity;

class m170101_000001_product extends CreateEntity
{
    public $createdByColumn = 'creation_user';
    public $createdAtColumn = 'creation_date';
    public $updatedByColumn = 'edition_user';
    public $updatedAtColumn = 'edition_date';

    public function getTableName()
    {
        return 'product';
    }

    public function columns()
    {
        return [
            'id' => $this->prymariKey()->...,
            'name' => ...,
        ];
    }
}

roaresearch\yii2\rmdb\migrations\CreatePersistentEntity

持久实体在用户删除后仍保留在数据库中。

yii2tech/ar-softdelete 提供了对该功能的支持。

CreateEntity 扩展了前面的类,添加了属性 $deletedByColumn$deletedAtColumn 和方法 deletedByDefinition()deletedAtDefinition() 来存储删除记录的用户和日期时间。

use roaresearch\yii2\rmdb\migrations\CreatePersistentEntity;

class m170101_000001_sale extends CreatePersistentEntity
{
    public $createdByColumn = 'creation_user';
    public $createdAtColumn = 'creation_date';
    public $updatedByColumn = 'edition_user';
    public $updatedAtColumn = 'edition_date';
    public $deletedByColumn = 'deletion_user';
    public $deletedAtColumn = 'deletion_date';

    public function getTableName()
    {
        return 'product';
    }

    public function columns()
    {
        return [
            'id' => $this->prymariKey()->...,
            'store_id' => ...,
        ];
    }
}

RMDB 模块

此库使用自定义模块来帮助以一致的方式配置所有扩展模型。

yii-app-advanced 中的 common\config\main.phpyii-app-basic 中的 common\config.php 中进行配置。

use roaresearch\yii2\rmdb\Module as RmdbModule;

return [
    // ...
    'modules' => [
        'rmdb' => [
            'class' => RmdbModule::class,
            'timestampClass' => ..., // optional
            'blameableClass' => ..., // optional
            'softDeleteClass' => ..., // optional
            'timestampValue' => time(), // optional by default uses `now()`
            'defaultUserId' => 5, // optional
        ],
    ],
];

模型

与迁移类似,模型也有 3 个类。

roaresearch\yii2\rmdb\models\Pivot

添加受保护的属性 $createdByAttribute$createdAtAttribute 来配置属性名称。该类将自动加载所需的行为并将它们配置为使用此属性提供的属性。

class ProductSale extends \roaresearch\yii2\rmdb\models\Pivot
{
    protected $createdByAttribute = 'creation_user';
    protected $createdAtAttribute = 'creation_date';

    // rest of model methods and logic
}

roaresearch\yii2\rmdb\models\Entity

扩展了前面的类,并添加受保护的属性 $updatedByAttribute$updatedAtAttribute 来配置属性名称。该类将自动加载所需的行为并将它们配置为使用此属性提供的属性。

class Product extends \roaresearch\yii2\rmdb\models\Entity
{
    protected $createdByAttribute = 'creation_user';
    protected $createdAtAttribute = 'creation_date';
    protected $updatedByAttribute = 'edition_user';
    protected $updatedAtAttribute = 'edition_date';

    // rest of model methods and logic
}

roaresearch\yii2\rmdb\models\PersistentEntity

扩展了前面的类,并添加受保护的属性 $deletedByAttribute$deletedAtAttribute 来配置属性名称。该类将自动加载所需的行为并将它们配置为使用此属性提供的属性。

class Product extends \roaresearch\yii2\rmdb\models\PersistentEntity
{
    protected $createdByAttribute = 'creation_user';
    protected $createdAtAttribute = 'creation_date';
    protected $updatedByAttribute = 'edition_user';
    protected $updatedAtAttribute = 'edition_date';
    protected $deletedAtAttribute = 'deletion_user';
    protected $deletedAtAttribute = 'deletion_date';

    // rest of model methods and logic
}

此模型使用 SoftDeleteActiveQuery 类,该类添加了 2 个方法来根据记录的删除状态进行筛选。

  • deleted()
  • notDeleted()

这些方法将使用列 $deletedByAttribute$deletedAtAttribute 来确定记录是否被软删除。为了防止连接中的冲突,属性将使用查询别名进行归一化。因此,必须在 deleleted()notDeleted() 调用之前调用 alias() 方法

Product::find()
  ->alias('p')
  ->notDeleted()
  ->innerJoin('brand b')

测试环境

此库使用 Composer Utils 来快速部署所需的数据库和测试环境。

git clone https://github.com/ROAResearch/yii2-rmdb.git
cd yii2-rmdb/
composer deploy

这将询问数据库凭据,验证它们并创建所需的数据库和结构。