定制 yii2tech/ar-dynattribute 二次开发

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

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

yii2tech/ar-dynattribute

最新稳定版本:1.0.2

Composer 安装命令:

composer require yii2tech/ar-dynattribute

包简介

Provides dynamic ActiveRecord attributes stored into the single field in serialized state

README 文档

README

12951949

ActiveRecord Dynamic Attribute Extension for Yii2


This extension provides dynamic ActiveRecord attributes stored into the single field in serialized state.

For license information check the LICENSE-file.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist yii2tech/ar-dynattribute

or add

"yii2tech/ar-dynattribute": "*"

to the require section of your composer.json.

Usage

This extension provides dynamic ActiveRecord attributes stored into the single field in serialized state. For example: imagine we create a web site, where logged in user may customize its appearance, like changing color schema or enable/disable sidebar and so on. In order to make this customization persistent all user's choices should be stored into the database. In general each view setting should have its own column in the 'user' table. However, this is not very practical in case your application is under development and new settings appear rapidly. Thus it make sense to use single text field, which will store all chosen view parameters in the serialized string. If new option introduced there will no necessity to change 'user' table schema. Migration for the 'user' table creation may look like following:

class m??????_??????_create_user extends \yii\db\Migration
{
    public function up()
    {
        $this->createTable('User', [
            'id' => $this->primaryKey(),
            'username' => $this->string()->notNull(),
            'email' => $this->string()->notNull(),
            'passwordHash' => $this->string()->notNull(),
            // ...
            'viewParams' => $this->text(), // field, which stores view parameters in serialized state
        ]);
    }

    public function down()
    {
        $this->dropTable('User');
    }
}

Heads up! In general such data storage approach is a bad practice and is not recommended to be used. Its main drawback is inability to use dynamic attributes in condition for the search query. It is acceptable only for the attributes, which are directly set and read for single record only, and never used for the filter queries.

Tip: you may store dynamic attributes into 'JSON' type column instead of plain text, in case you are using modern DBMS with built-in JSON support (e.g. MySQL >= 5.5 or PostgreSQL), however, you will have to deal with possible search condition composition on your own - this extension does not provide explicit support for it.

This extension provides [[\yii2tech\ar\dynattribute\DynamicAttributeBehavior]] ActiveRecord behavior for the dynamic attributes support. For example:

use yii\db\ActiveRecord;
use yii2tech\ar\dynattribute\DynamicAttributeBehavior;

class User extends ActiveRecord
{
    public function behaviors()
    {
        return [
            'dynamicAttribute' => [
                'class' => DynamicAttributeBehavior::className(),
                'storageAttribute' => 'viewParams', // field to store serialized attributes
                'dynamicAttributeDefaults' => [ // default values for the dynamic attributes
                    'bgColor' => 'green',
                    'showSidebar' => true,
                ],
            ],
        ];
    }

    public static function tableName()
    {
        return 'User';
    }

    // ...
}

Once being attached [[\yii2tech\ar\dynattribute\DynamicAttributeBehavior]] allows its owner to operate dynamic attributes just as regular one. On model save they will be serialized and stored into the holding field. After record is fetched from database the first attempt to read the dynamic attributes will unserialize them and prepare for the usage. For example:

$model = new User();
// ...
$model->bgColor = 'red';
$model->showSidebar = false;
$model->save(); // 'bgColor' and 'showSidebar' are serialized and stored at 'viewParams'
echo $model->viewParams; // outputs: '{"bgColor": "red", "showSidebar": false}'

$refreshedModel = User::findOne($model->getPrimaryKey());
echo $refreshedModel->bgColor; // outputs 'red'
echo $refreshedModel->showSidebar; // outputs 'false'

You may use dynamic attributes as the regular ActiveRecord attributes. For example: you may specify the validation rules for them and obtain their values via web form.

Note: keep in mind that dynamic attributes do not correspond to ActiveRecord entity fields, thus some particular ActiveRecord methods like updateAttributes() will not work for them.

Default values setup

As you may note from above example, you can provide a default values for the dynamic attributes via [[\yii2tech\ar\dynattribute\DynamicAttributeBehavior::$dynamicAttributeDefaults]]. Thus once you need extra dynamic attribute for your model you can just update the dynamicAttributeDefaults list with corresponding value, without necessity to perform any updates on your database.

class User extends ActiveRecord
{
    public function behaviors()
    {
        return [
            'dynamicAttribute' => [
                'class' => DynamicAttributeBehavior::className(),
                'storageAttribute' => 'viewParams',
                'dynamicAttributeDefaults' => [
                    'bgColor' => 'green',
                    'showSidebar' => true,
                    'fontColor' => 'black', // newly added attribute
                ],
            ],
        ];
    }

    // ...
}

$newModel = new User();
echo $newModel->bgColor; // outputs 'green'
echo $newModel->showSidebar; // outputs 'true'

$oldModel = User::find()->orderBy(['id' => SORT_ASC])->limit(1)->one();
echo $oldModel->viewParams; // outputs: '{"bgColor": "red", "showSidebar": false}'
echo $oldModel->fontColor; // outputs: 'black'

Note: you may exclude dynamic attribute, which value equals the default one, from saving disabling [[\yii2tech\ar\dynattribute\DynamicAttributeBehavior::$saveDynamicAttributeDefaults]] option.

Restrict dynamic attribute list

Setup of the dynamic attribute default values not only useful, but in general is necessary. This list puts a restriction on the possible dynamic attribute names. Only attributes, which have default value specified can be set or read from the model. This prevents the possible mistakes caused by typos in the code. For example:

$newModel = new User();
$newModel->bgColor = 'blue'; // works fine
$newModel->unExistingAttribute = 10; // throws an exception!

However sometimes there is necessity of storage list of attributes, which can not be predicted. For example, saving response fields from some external service. In this case you can disable check performed on attribute setter using [[\yii2tech\ar\dynattribute\DynamicAttributeBehavior::$allowRandomDynamicAttribute]]. If it is set to true you will be able to setup any dynamic attribute no matter declared or not at dynamicAttributeDefaults.

Note: you can also use [[\yii2tech\ar\dynattribute\DynamicAttributeBehavior::setDynamicAttributes()]] method to bypass naming restriction. This method will set all provided attributes without any checks.

You can as well control the dynamic attributes list to be actually saved using [[\yii2tech\ar\dynattribute\DynamicAttributeBehavior::$dynamicAttributeSaveFilter]]. If set to true it will exclude any attribute, which is not listed at dynamicAttributeDefaults option. You may as well specify it as a PHP callback, which will perform some custom filtering. This option allows you to remove obsolete dynamic attributes, which existed in the past, but no longer actual.

Serializer setup

By default [[\yii2tech\ar\dynattribute\DynamicAttributeBehavior]] saves the dynamic attribute in JSON format. However, you may setup another serializer for them via [[\yii2tech\ar\dynattribute\DynamicAttributeBehavior::$serializer]]. The following serializers are available withing this extension:

  • [[\yii2tech\ar\dynattribute\JsonSerializer]] - stores data in JSON format
  • [[\yii2tech\ar\dynattribute\PhpSerializer]] - stores data using PHP serialize()/unserialize() functions
  • [[\yii2tech\ar\dynattribute\CallbackSerializer]] - stores data via custom serialize PHP callback.
  • [[\yii2tech\ar\dynattribute\JsonExpressionSerializer]] - handles [[yii\db\JsonExpression]] instances, supporting usage of 'JSON' DB column types.

Please refer to the particular serializer class for more details.

yii2tech/ar-dynattribute 适用场景与选型建议

yii2tech/ar-dynattribute 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.42k 次下载、GitHub Stars 达 43, 最近一次更新时间为 2016 年 08 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 yii2tech/ar-dynattribute 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 43
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2016-08-25