jorisvaesen/cakephp-keyvalue-pairs

CakePHP 插件,用于处理在数据源和应用之间映射的键值对

3.0.0 2016-03-28 18:25 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:36:51 UTC


README

Build Status Coverage Status License Latest Stable Version

在数据源和应用之间映射键值对。

要求

  • PHP 5.4+
  • CakePHP 3.x

安装

使用 composer

运行以下命令

composer require jorisvaesen/cakephp-keyvalue-pairs:"~3.0"

或者将最新版本的 json 片段复制到您的项目的 composer.json 文件中

{
    "require": {
        "jorisvaesen/cakephp-keyvalue-pairs": "~3.0"
    }
}

您需要启用插件

bin/cake plugin load JorisVaesen/KeyValuePairs

或者将此行复制到您的 config/bootstrap.php 文件中

Plugin::load('JorisVaesen/KeyValuePairs');

如果您已经使用了 Plugin::loadAll();,则此步骤不是必需的。

使用方法

添加行为

$this->addBehavior('JorisVaesen/KeyValuePairs.KeyValuePairs', [
    // Here you can override the default options
]);

选项

可用函数

  • getPair($key, $asEntity = false) 获取 $key 的值。当 $asEntity 设置为 true 时,它返回完整的实体(当您想对其进行修改时很有用)。
  • getPairs($keys, $requireAll = true, $asEntity = false) 返回一个包含键及其值的关联数组。当 $requireAll 设置为 true 时,如果找不到所有键,则函数返回 false。当 $asEntity 设置为 true 时,它返回完整的实体(当您想对其进行修改时很有用)。

提示

  • 当您使用 updateAll 时,缓存失效发生在 afterSave 和 afterDelete 回调之后,这些回调不会调用。在这种情况下,您应该自己使缓存失效。
  • 建议使用缓存,其持续时间可以设置为 +999 天,因为当键值对被保存或删除时,缓存的输出会自动失效。
  • 缓存会自动将所有键值对保存到数据库中,并从中提取特定值。如果您想要每个记录的缓存文件,请在此插件中禁用缓存,自行进行缓存或通过发起拉取请求来建议此功能。
  • 此插件相对较新,可能包含错误。如果您发现任何错误或想提出改进建议,请使用问题跟踪器 此处

示例

假设您有一个应用程序,用户可以创建发票,这些发票在创建时应该获得前缀和编号,但此用户想管理这些值以用于新发票。

首先,我们创建一个数据库表来存储键值对,并插入默认值。

CREATE TABLE `configs` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `key` varchar(255) NOT NULL,
    `value` VARCHAR(255) NOT NULL,
    `is_deleted` TINYINT(1) NOT NULL DEFAULT 0,
    `modified` datetime NOT NULL,
    PRIMARY KEY (`id`),
    INDEX `key_index` (`key`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

INSERT INTO `configs` (`key`, `value`, `is_deleted`) VALUES ('invoice_prefix', 'INV-', 0);
INSERT INTO `configs` (`key`, `value`, `is_deleted`) VALUES ('invoice_next_number', '2016001', 0);

然后我们创建一个缓存配置,该配置应由插件使用。

Cache::config('pairs', [
    'className' => 'File',
    'duration' => '+999 days',  // cache gets invalidated automatically when a pair is saved or removed
    'path' => CACHE,
    'prefix' => 'pairs_'
]);

接下来,我们在 Model/Table/ConfigsTable.php 中添加行为到我们的表。

public function initialize(array $config) 
{
    ...
    
    $this->addBehavior('JorisVaesen/KeyValuePairs.KeyValuePairs', [
        'fields' => [               //  We just leave this the default
            'key' => 'key',
            'value' => 'value'
        ],
        'cache' => true,            //  Enable caching
        'cacheConfig' => 'pairs',   //  Tell the plugin to use the pairs cache config
        'scope' => [                //  Just as example to show how to use extra conditions when fetching pairs
            'is_deleted' => false
        ],
        'preventDeletion' => true,  //  Prevents us from deleting any record in this table (and thereby possibly break the app)
        'allowedKeys' => [          //  Prevents us from saving any other key than the ones specified here
            'invoice_prefix',
            'invoice_next_number'
        ]
    ]);
}

现在,当创建新的发票时,我们可以获取值并增加发票编号(这将自动使缓存失效)。

public function add() 
{
    ...
    
    $pairsTable = TableRegistry::get('Configs');
    // We set $requireAll and $asEntity to true to be sure all keys are there and we can make changes to it later
    $pairs = $pairsTable->getPairs(['invoice_prefix', 'invoice_postfix'], true, true);
    
    if (!$pairs) {
        // throw error
    }
    
    $invoice->number = $pairs['invoice_prefix']->value . $pairs['invoice_next_number']->value;
    
    if ($this->Invoices->save($invoice)) {
        $pairs['invoice_next_number']->value = (int)$pairs['invoice_next_number']->value + 1; 
        $pairsTable->save($pairs['invoice_next_number']);
    }
    
    ...
}

许可协议

MIT 许可证 (MIT)

版权所有 (c) 2016 Joris Vaesen

特此授予任何获得此软件及其相关文档副本(以下简称“软件”)的人免费使用软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许向软件提供方提供软件的人行使上述权利,但须遵守以下条件:

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

本软件按“现状”提供,不提供任何形式的保证,无论是明示的、暗示的,包括但不限于适销性、特定用途适用性和非侵权性。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论是基于合同行为、侵权或其他方式,无论索赔、损害或其他责任是否与软件或其使用或其他方式相关。