tecnocen/yii2-rmdb

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

1.0.1 2019-06-05 21:36 UTC

This package is auto-updated.

Last update: 2024-08-29 04:58:49 UTC


README

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

安装

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

composer require tecnocen/yii2-rmdb

或编辑composer.json文件

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

使用方法

创建迁移

对于每种类型的RMDB表,都有3个迁移类。

tecnocen\rmdb\migrations\CreatePivot

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

class m170101_000001_product_sale extends \tecnocen\rmdb\migrations\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'];
    }
}

tecnocen\rmdb\migrations\CreateEntity

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

class m170101_000001_product extends \tecnocen\rmdb\migrations\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' => ...,
        ];
    }
}

tecnocen\rmdb\migrations\CreatePersistentEntity

持久实体在用户删除后仍然存储在数据库中。

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

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

class m170101_000001_sale extends \tecnocen\rmdb\migrations\CreateEntity
{
    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 tecnocen\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个类。

tecnocen\rmdb\models\Pivot

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

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

    // rest of model methods and logic
}

tecnocen\rmdb\models\Entity

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

class Product extends \tecnocen\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
}

tecnocen\rmdb\models\PersistentEntity

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

class Product extends \tecnocen\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
}