定制 roaresearch/yii2-rmdb 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

roaresearch/yii2-rmdb

Composer 安装命令:

composer require roaresearch/yii2-rmdb

包简介

Yii 2 Library to create RMDB models and migrations

README 文档

README

Library with migrations and models to easily create RMDB tables and models.

Installation

You can use composer to install the library roaresearch/yii2-rmdb by running the command;

composer require roaresearch/yii2-rmdb

or edit the composer.json file

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

Usage

Create Migrations

There are 3 migration classes for each type of RMDB tables.

roaresearch\yii2\rmdb\migrations\CreatePivot

Uses the properties $createdByColumn, $createdAtColumn and methods createdByDefinition(), createdAtDefinition() to store the user and datetime a record was created.

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

Extends the previous class adding the properties $updatedByColumn, $updatedAtColumn and methods updatedByDefinition(), updatedAtDefinition() to store the user and datetime a record was updated.

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

A persistent entity remains stored in the database after the user deletes it.

The library yii2tech/ar-softdelete provides support for this functionality.

CreateEntity extends the previous class adding the properties $deletedByColumn, $deletedAtColumn and methods deletedByDefinition(), deletedAtDefinition() to store the user and datetime a record was deleted.

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 Module

This library uses a custom module to help configure all the extended models in an homologued way.

configure it in your common\config\main.php in yii-app-advanced and common\config.php in yii-app-basic.

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
        ],
    ],
];

Models

Like the migrations there are 3 classes for models.

roaresearch\yii2\rmdb\models\Pivot

Adds protected properties $createdByAttribute and $createdAtAttribute to configure the names of the attributes. The class will automatically load the needed behaviors and configure them to use the attributes as provided by this properties.

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

Extends the previous class and adds protected properties $updatedByAttribute and $updatedAtAttribute to configure the names of the attributes. The class will automatically load the needed behaviors and configure them to use the attributes as provided by this properties.

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

Extends the previous class and adds protected properties $deletedByAttribute and $deletedAtAttribute to configure the names of the attributes. The class will automatically load the needed behaviors and configure them to use the attributes as provided by this properties.

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
}

This model uses the SoftDeleteActiveQuery class which add 2 methods to filter based on the deletion status of a record.

  • deleted()
  • notDeleted()

This methods will use columns $deletedByAttribute and $deletedByAttribute to determine if a record was soft deleted or not. To prevent conflicts in joins the attributes are normalized with the query alias. For this reason the alias() method must be called before the deleleted() or notDeleted().

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

Testing Environment

This library use Composer Utils to quickly deploy the needed database and testing Environment.

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

This will ask db credentials, validate them and create the needed database and structure.

roaresearch/yii2-rmdb 适用场景与选型建议

roaresearch/yii2-rmdb 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.23k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「migration」 「migrate」 「advanced」 「yii2」 「rmdb」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 roaresearch/yii2-rmdb 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 roaresearch/yii2-rmdb 我们能提供哪些服务?
定制开发 / 二次开发

基于 roaresearch/yii2-rmdb 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.23k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2019-10-01