定制 zssarkany/laravel-database-sticky-timezone 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zssarkany/laravel-database-sticky-timezone

最新稳定版本:v1.0.4

Composer 安装命令:

composer require zssarkany/laravel-database-sticky-timezone

包简介

Service for keeping database connection timezone synchronized to PHP default timezone

README 文档

README

Latest Version on Packagist Software License Build Status StyleCI Scrutinizer Code Quality Code Coverage Open Issues Total Downloads

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.

统计信息

  • 总下载量: 357
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-06-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固