zssarkany/laravel-database-sticky-timezone
Composer 安装命令:
composer require zssarkany/laravel-database-sticky-timezone
包简介
Service for keeping database connection timezone synchronized to PHP default timezone
README 文档
README
Service for keeping database connection timezone synchronized to PHP default timezone.
Purpose
There are at least two different approaches for handling database fields of
timestamp type in projects:
- fetch data using some predefined timezone offset and adjust resulted string representation values with JavaScript or PHP;
- adjust database connection by setting its timezone offset, then fetch timestamp values with desired string representation.
Obviously there are pros and cons for both approaches, but if there is a requirement to support LIKE matching (e.g.: 2019-06-% or %19-06%), the first approach sems to be quite challenging.
This package aims to implement the second approach.
Storage engines store these values as 32bits unsigned integers, and string representation (YYYY-MM-DD HH:MM:SS) depends on the connection-specific timezone offset. Select, update, where expressions could provide unexpected results without appropriate caution.
Under the hood Illuminate\Database\MySqlConnection gets extended and getPdo()
and getReadPdo() methods get overwritten by a change detector and actuator logic.
When mentioned methods are invoked, underlying PDO object gets checked for
replacement and PHP default timezone offset (date('P')) gets compared to
previously set offset value. In case of change is detected, 'SET time_zone = "+hh:mm"'
is executed on the specific PDO instance, where +hh:mm equals to date('P').
This makes developer able to simply update PHP default time zone with
date_default_timezone_set(...) and database queries executed afterwards
will be affected by the new offset. Effect is limited for timestamp fields,
datetime is not affected.
Requirements
- PHP >= 7.1
- Laravel >= 5.5
See PHP and Laravel unit and integration tests
Install
Via Composer
$ composer require zssarkany/laravel-database-sticky-timezone
It is not necessary to add service provider to config/app.php, but if service
discovery is disabled via composer.json,
ZsSarkany\LaravelDatabaseStickyTimezone\DatabaseStickyTimezoneServiceProvider::class
needs to be added before Illuminate\Database\DatabaseServiceProvider::class to
providers array, otherwise the the connection resolver will be bound after
the database connection is resolved already.
Usage
Append sticky_timezone => true to mysql connection configuration in
config/database.php to enable automatic timezone adjustment.
Example:
return [ // ... 'connections' => [ // ... 'mysql' => [ // ... 'sticky_timezone' => true, ], // ... ], // ... ];
SQL statements executed on this connection are taking PHP default timezone into account for timestamp fields.
PHP and Laravel unit and integration tests
Package is tested with the following conditions:
| PHP | Laravel | Stability |
|---|---|---|
| 7.1 | 5.5 | stable |
| 7.1 | 5.6 | stable |
| 7.1 | 5.7 | stable |
| 7.1 | 5.8 | stable |
| 7.2 | 5.5 | stable |
| 7.2 | 5.6 | stable |
| 7.2 | 5.7 | stable |
| 7.2 | 5.8 | stable |
| 7.2 | 6.0 | stable |
| 7.2 | 6.2 | stable |
| 7.3 | 5.5 | stable |
| 7.3 | 5.6 | stable |
| 7.3 | 5.7 | stable |
| 7.3 | 5.8 | stable |
| 7.3 | 6.0 | stable |
| 7.3 | 6.2 | stable |
Supported drivers
Package currently support MySQL driver only, but it should be easy to implement support for other drivers, which are provided by Laravel.
Contributions are welcome :)
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email zsolt.sarkany@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
zssarkany/laravel-database-sticky-timezone 适用场景与选型建议
zssarkany/laravel-database-sticky-timezone 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 390 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「zssarkany」 「laravel-database-sticky-timezone」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zssarkany/laravel-database-sticky-timezone 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zssarkany/laravel-database-sticky-timezone 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 390
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-06-03