vitalyspirin/yii2-simpleactiverecord
Composer 安装命令:
composer require vitalyspirin/yii2-simpleactiverecord
包简介
Extension of Yii2 ActiveRecord with added automatically generaged validators
README 文档
README
Extension of Yii 2 ActiveRecord with automatically generated validators.
Quick Start
To install it you can just download zip or use composer:
composer require vitalyspirin/yii2-simpleactiverecord
To use it in code:
use vitalyspirin\yii2\simpleactiverecord\SimpleActiveRecord; class T1 extends SimpleActiveRecord { }
There is no need to overload rules() function to specify validators. Validators will be added automatically by class constructor based on table schema. However in current version relations (based on foreign keys) will not be added.
There are two ways to create an instance of such class. Constructor of SimpleActiveRecord takes one boolean parameter: $maximumValidation. If it's 'false' then only those validators will be added that are generated by Gii. In terms of above example it will look like this:
$t1 = new T1(false);
If to pass 'true' (which is also default) then maximum validation functionality will be added. It includes ranges for integer, ranges for enum, pattern for time etc. Of course you can see those validators:
$t1 = new T1(); var_dump($t1->rules());
If table schema changed then validators will be adjusted automatically.
Examples
Let's say we have the following SQL schema:
CREATE TABLE person ( person_id INT PRIMARY KEY AUTO_INCREMENT, person_firstname VARCHAR(35) NOT NULL, person_lastname VARCHAR(35) NOT NULL, person_gender ENUM('male', 'female'), person_dob DATE NULL, person_salary DECIMAL UNSIGNED );
then if we run the following code:
use vitalyspirin\yii2\simpleactiverecord\SimpleActiveRecord; class Person extends SimpleActiveRecord { // totally empty class } $person = new Person(false /*Gii style validation only*/ ); var_dump($person->rules());
we will get the following output:
array (size=5)
0 =>
array (size=2)
0 =>
array (size=2)
0 => string 'person_firstname' (length=16)
1 => string 'person_lastname' (length=15)
1 => string 'required' (length=8)
1 =>
array (size=2)
0 =>
array (size=1)
0 => string 'person_salary' (length=13)
1 => string 'number' (length=6)
2 =>
array (size=2)
0 =>
array (size=1)
0 => string 'person_dob' (length=10)
1 => string 'safe' (length=4)
3 =>
array (size=3)
0 =>
array (size=2)
0 => string 'person_firstname' (length=16)
1 => string 'person_lastname' (length=15)
1 => string 'string' (length=6)
'max' => int 35
4 =>
array (size=2)
0 =>
array (size=1)
0 => string 'person_gender' (length=13)
1 => string 'string' (length=6)
Above validators are the same that would be generated by Gii module of Yii. Additional validators can be generated if to call class constructor without false parameter:
$person = new Person(); var_dump($person->rules());
In this case the output will be the following (pay attention to 'person_gender', 'person_dob' and 'person_salary' fields):
array (size=5)
0 =>
array (size=2)
0 =>
array (size=2)
0 => string 'person_firstname' (length=16)
1 => string 'person_lastname' (length=15)
1 => string 'required' (length=8)
1 =>
array (size=3)
0 =>
array (size=1)
0 => string 'person_gender' (length=13)
1 => string 'in' (length=2)
'range' =>
array (size=2)
0 => string 'male' (length=4)
1 => string 'female' (length=6)
2 =>
array (size=5)
0 =>
array (size=1)
0 => string 'person_dob' (length=10)
1 => string 'date' (length=4)
'format' => string 'yyyy-MM-dd' (length=10)
'min' => string '1000-01-01' (length=10)
'max' => string '9999-12-31' (length=10)
3 =>
array (size=3)
0 =>
array (size=1)
0 => string 'person_salary' (length=13)
1 => string 'number' (length=6)
'min' => int 0
4 =>
array (size=3)
0 =>
array (size=2)
0 => string 'person_firstname' (length=16)
1 => string 'person_lastname' (length=15)
1 => string 'string' (length=6)
'max' => int 35
You can also pass properties to contructor:
$person = new Person(['person_firstname' => 'John', 'person_lastname' => 'Smith']); echo $person->person_firstname;
In the case above maximum validation will be chosen by default. You can specify validation level explicitly (Gii style validation in the example below):
$person = new Person(false, ['person_firstname' => 'John', 'person_lastname' => 'Smith']); echo $person->person_firstname;
Additional features
This extension of Active Record has also function getEnumValues() that shows enum values for table column. The following code
$person = new Person(); var_dump($person->getEnumValues());
will return
array (size=1)
'person_gender' =>
array (size=2)
0 => string 'male' (length=4)
1 => string 'female' (length=6)
You can also see tests in SimpleActiveRecordTest.php for simple examples.
vitalyspirin/yii2-simpleactiverecord 适用场景与选型建议
vitalyspirin/yii2-simpleactiverecord 是一款 基于 HTML 开发的 Composer 扩展包,目前已累计 966 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 05 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Active Record」 「activerecord」 「simpleactiverecord」 「simple active record」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vitalyspirin/yii2-simpleactiverecord 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vitalyspirin/yii2-simpleactiverecord 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vitalyspirin/yii2-simpleactiverecord 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
ActiveRecord for API
Propel2 is an open-source Object-Relational Mapping (ORM) for PHP 5.5 and up.
CalendarView widget for Yii 2 Framework.
PHP Interface for Babel Street Text Analytics
A Symfony extension to get active class base on current bundle/controller/action
LDAP user provider bundle for Symfony 6.4
统计信息
- 总下载量: 966
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2016-05-16