tecnocen / yii2-rmdb
用于创建RMDB模型和迁移的Yii 2库
Requires
- php: >=5.5
- tecnocen/yii2-migrate: *
- yiisoft/yii2: ~2.0.10
Requires (Dev)
- codeception/base: ^2.2.3
- codeception/verify: ~0.3.1
Suggests
- yii2tech/ar-softdelete: Needed for Persistent Entities
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.php
和yii-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 }