yiiplus/yii2-kvstore

kvstore 是以 key->value 数据结构进行存储

2.1.6 2019-01-30 08:52 UTC

README

在YiiPlus中提供了kvstore存储的方式来对数据量大的情况下进行优化。kvstore 是以 key->value 数据结构进行存储.

1.安装

安装此扩展的首选方法是通过 composer.

执行命令

php composer.phar require --prefer-dist yiiplus/yii2-kvstore "^2.0.0"

或添加配置到项目目录下的composer.json

"require": {
    ...
    "yiiplus/yii2-kvstore": "^2.0.0",
    ...
}

2.配置

'modules' => [
    'kvstore' => [
        'class' => 'yiiplus\kvstore\Module',
        'sourceLanguage' => 'en'
    ],
    ...
],

...

'components' => [
    'i18n' => [
        'translations' => [
            '*' => [
                'class' => 'yii\i18n\PhpMessageSource'
            ],
        ],
    ],
    'kvstore' => [
        'class' => 'yiiplus\kvstore\Kvstore'
    ],
    ...
]

3.创建数据

./yii migrate --migrationPath=@yiiplus/kvstore/migrations

4.使用

快速使用

$kvstore = Yii::$app->kvstore;

$value = $kvstore->get('group.key');
$value = $kvstore->get('key', 'group');

$kvstore->set('group.key', 'value');
$kvstore->set('group.key', 'value');
$kvstore->set('key', 'value', 'group');

自定义MVC模型

// Model
class Site extends Model {
    public $siteName, $siteDescription;
    public function rules()
    {
        return [
            [['siteName', 'siteDescription'], 'string'],
        ];
    }
    
    public function fields()
    {
            return ['siteName', 'siteDescription'];
    }
    
    public function attributes()
    {
            return ['siteName', 'siteDescription'];
    }
}

//Controller
function actions(){
   return [
        ....
            'site-kvstore' => [
                'class' => 'yiiplus\kvstore\actions\KvstoreAction',
                'modelClass' => 'app\models\Site',
                //'group' => 'site',
                //'scenario' => 'kvstore',
                'viewName' => 'site-kvstore'
            ],
        ....
    ];
}

// Views
<?php $form = ActiveForm::begin(['id' => 'site-kvstore-form']); ?>
<?php echo $form->field($model, 'siteName'); ?>
<?php echo $form->field($model, 'siteDescription'); ?>
<div class="form-group">
    <?php echo Html::submitButton(Module::t('保存'), ['class' => 'btn btn-primary']); ?>
</div>
<?php ActiveForm::end(); ?>