elstc/cakephp-time-interval
Composer 安装命令:
composer require elstc/cakephp-time-interval
包简介
TimeInterval type plugin for CakePHP
README 文档
README
This plugin provides time_interval custom type for MySQL's TIME, Postgres's INTERVAL,
and provides time_interval_int custom type for seconds as INTEGER.
This is a custom type to represent intervals, which CakePHP can treat as a TimeInterval object that inherits from DateInterval.
Version Map
| CakePHP Version | Plugin Version | Branch |
|---|---|---|
| 5.x | 3.x | cake5 |
| 4.x | 2.x | cake4 |
| 3.x | 0.3.x | cake3 |
Installation
You can install this plugin into your CakePHP application using composer.
The recommended way to install composer packages is:
composer require elstc/cakephp-time-interval
Load plugin
Load the plugin by adding the following statement in your project's src/Application.php:
$this->addPlugin('Elastic/TimeInterval');
Usage
Add column definitions to Table class
use Cake\Database\Schema\TableSchema; class WorkTimesTable extends Table { protected function _initializeSchema(TableSchema $schema) { parent::_initializeSchema($schema); $schema->setColumnType('duration', 'time_interval'); // If your column type is seconds as INTEGER, Use `time_interval_int` instead. $schema->setColumnType('duration_sec', 'time_interval_int'); return $schema; } }
Add column validation to Table class
Use timeInterval rule instead of time.
The timeInterval rule is in the timeInterval validation provider.
use Cake\Validation\Validator; use Elastic\TimeInterval\Validation\TimeIntervalValidation; class WorkTimesTable extends Table { public function validationDefault(Validator $validator) { // ... $validator->add('duration', 'timeInterval', [ 'rule' => 'timeInterval', 'provider' => 'timeInterval', ]); return $validator; } }
Adding a mutator to the Entity class is useful
use Cake\Database\Type; class WorkTime extends Entity { protected function _setDuration($value) { // convert to TimeInterval return Type::build('time_interval')->marshal($value); } } $workTime->duration = '00:15:00'; $workTime->duration = ($startTime)->diff($endTime); // $startTime, $endTime are DateTime objects. $workTime->duration = 3600; // as seconds
NOTE
MySQL TIME column limitation
MySQL :: MySQL 8.0 Reference Manual :: 13.2.3 The TIME Type
By default, values that lie outside the TIME range but are otherwise valid are clipped to the closest endpoint of the range. For example,
'-850:00:00' and '850:00:00' are converted to '-838:59:59' and '838:59:59'. Invalid TIME values are converted to '00:00:00'.
Note that because '00:00:00' is itself a valid TIME value, there is no way to tell, from a value of '00:00:00' stored in a table,
whether the original value was specified as '00:00:00' or whether it was invalid.
DateInterval / TimeInterval constructed with date parts will have incorrect time interpretation
If you initialize DateInterval with date parts, time will not be interpreted correctly.
$workTime->duration = new DateInterval('PT75H4M5S'); // OK $workTime->duration = new DateInterval('P1M2DT3H4M5S'); // can't get expected time
elstc/cakephp-time-interval 适用场景与选型建议
elstc/cakephp-time-interval 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.2k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 elstc/cakephp-time-interval 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 elstc/cakephp-time-interval 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-18