roaresearch / yii2-rmdb
用于创建 RMDB 模型和迁移的 Yii 2 库
Requires
- php: ~8.1
- roaresearch/yii2-migrate: ~3.0.0
- yiisoft/yii2: ~2.0.27
Requires (Dev)
- ext-mbstring: *
- codeception/codeception: ~5.0.0
- codeception/module-asserts: dev-master
- codeception/module-yii2: *
- codeception/verify: ~1.2.0
- roaresearch/composer-utils: *
- yiisoft/yii2-debug: ~2.1.0
Suggests
- yii2tech/ar-softdelete: Needed for Persistent Entities
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.php
和 yii-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
这将询问数据库凭据,验证它们并创建所需的数据库和结构。