jmjjg/cakephp3-database

CakePHP 3 插件,提供从数据库表模式添加默认验证规则和清理保存数据的相关类。

安装: 22

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 1

开放问题: 2

类型:cakephp-plugin

1.0.0-RC1 2017-01-22 20:14 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:20:08 UTC


README

描述

CakePHP 3 插件,提供从数据库表模式添加默认验证规则和清理保存数据的相关类。

默认启用缓存,但可以在配置或运行时禁用。

仅在 CakePHP 3.3.x 版本下运行良好(已测试 CakePHP 3.3.12)。

主要类

AutovalidateBehavior 从数据库表模式读取各种信息,例如列类型、列是否可以为 NULL、外键约束、唯一约束,并将这些验证规则自动添加到默认规则中。

FormattableBehavior 使用可配置的静态类方法在验证或保存之前清理数据。可以基于列类型和字段名正则表达式定义应用格式化程序的列。

设置

假设插件安装于 plugins/Database 目录下,请在 config/bootstrap.php 文件中添加以下内容:

Plugin::load('Database', ['autoload' => true]);

设置

以下全局设置可以添加到 config/app.php 文件中(可选)。

return [
    // ...
    'plugin' =>  [
        // Custom Database plugin behavior configuration
        'Database' => [
            'AutovalidateBehavior' => [
                'cache' => false
            ],
            'FormattableBehavior' => [
                'cache' => false
            ]
        ]
    ],
    // ...
];

用法

以下代码应在表类中的 initialize() 方法内添加。

这两个行为是独立的,可以按任意顺序加载。

注意,NULL 和布尔值 TRUE 在配置值中是等效的。

public function initialize(array $config)
{
    // ...
        $this->addBehavior('DatabaseAutovalidate',
            [
                'className' => 'Database.Autovalidate',
                // Default values
                // 1°) Accepted validator names, as a string or an array of strings, NULL for any
                'accepted' => null,
                // 2°) Cache validation rules and their error messages ? NULL for global settings
                'cache' => null,
                // 3°) Domain to use for error messages
                'domain' => 'database'
            ]
        );

        $this->addBehavior('DatabaseFormattable',
            [
                'className' => 'Database.Formattable',
                // Default values
                // 1°) Cache formatters and the field list they apply to ? NULL for global settings
                'cache' => null,
                // 2°) List of formatter functions or static methods as keys, fields they apply to as values
                    // a°) A NOT key is allowed to negate the condition
                    // b°) Boolean true means all fields, false means the formatter is not used
                    // c°) Strings or array of strings are allowed
                        // * regular expressions (delimited by "/") are used to filter field names
                        // * other strings are used to filter field types
                'formatters' => [
                    // Extract the part after the last "_" character
                    '\\Database\\Utility\\Formatter::formatSuffix' => '/_id$/',
                    // Trim the value
                    '\\Database\\Utility\\Formatter::formatTrim' => [ 'NOT' => 'binary'],
                    // Transform empty string to a NULL value
                    '\\Database\\Utility\\Formatter::formatNull' => true,
                    // Tries to parse an integer value using the current intl.default_locale value
                    '\\Database\\Utility\\Formatter::formatInteger' => ['integer', 'biginteger'],
                    // Tries to parse a decimal value using the current intl.default_locale value
                    '\\Database\\Utility\\Formatter::formatDecimal' => ['decimal', 'float', 'numeric']
                ]
            ]
        );
    // ...
}

代码质量

sudo bash -c "( rm -rf logs/quality ; find logs -type f -iname '*.log' -exec rm {} \;  ; find tmp -type f ! -name 'empty' -exec rm {} \; )"
sudo -u apache ant quality -f vendor/jmjjg/cakephp3-database/vendor/Jenkins/build.xml