janklan/simple-date-time-for-doctrine
Composer 安装命令:
composer require janklan/simple-date-time-for-doctrine
包简介
Doctrine DBAL types for simple-date-time value objects
README 文档
README
A Doctrine integration of janklan/simple-date-time.
Why?
Databases Doctrine integrates with support DATE and TIME data types, but Doctrine converts those values to a PHP's native \DateTimeImmutable objects, forcing additional complexity onto you when you don't want it: time zones, and a date when you only want to work with time and vice versa.
For example:
- Birthdays: A birthday is January 15th, not "January 15th in UTC-5"
- Special Days: The Australia Day is on January 26th, The Independence Day is on July 4th - regardless of which time zone in Australia or the U.S. you are
- Business hours: Standard business hours across a global enterprise could be 9am to 5pm, regardless of where on Earth do you work for the business: you have to rock up at 9am for work, wherever you are.
janklan/simple-date-time provides a solution at a PHP level, and this package provides the connecting dots between that solution and Doctrine.
Installation
composer require janklan/simple-date-time-for-doctrine
Configuration
This package provides four Doctrine DBAL types:
| Type Name | Doctrine Type | PHP Type |
|---|---|---|
simple_date_immutable |
SimpleDateImmutableType |
SimpleDateImmutable |
simple_date |
SimpleDateType |
SimpleDate |
simple_time_immutable |
SimpleTimeImmutableType |
SimpleTimeImmutable |
simple_time |
SimpleTimeType |
SimpleTime |
Doctrine (standalone)
Register the types before using them:
use Doctrine\DBAL\Types\Type; use JanKlan\SimpleDateTimeForDoctrine\SimpleDateImmutableType; use JanKlan\SimpleDateTimeForDoctrine\SimpleTimeImmutableType; Type::addType('simple_date_immutable', SimpleDateImmutableType::class); Type::addType('simple_time_immutable', SimpleTimeImmutableType::class);
Symfony
Add to config/packages/doctrine.yaml:
doctrine: dbal: types: simple_date_immutable: JanKlan\SimpleDateTimeForDoctrine\SimpleDateImmutableType simple_date: JanKlan\SimpleDateTimeForDoctrine\SimpleDateType simple_time_immutable: JanKlan\SimpleDateTimeForDoctrine\SimpleTimeImmutableType simple_time: JanKlan\SimpleDateTimeForDoctrine\SimpleTimeType
Usage
Once configured, use the types in your entity mappings:
use Doctrine\ORM\Mapping as ORM; use JanKlan\SimpleDateTime\SimpleDate; use JanKlan\SimpleDateTime\SimpleDateImmutable; use JanKlan\SimpleDateTime\SimpleTime; use JanKlan\SimpleDateTime\SimpleTimeImmutable; #[ORM\Entity] class Employee { #[ORM\Column(type: 'simple_date_immutable')] public SimpleDateImmutable $immutableBirthday; #[ORM\Column(type: 'simple_time_immutable')] public SimpleTimeImmutable $immutableShiftStart; #[ORM\Column(type: 'simple_date')] public SimpleDate $mutableBirthday; #[ORM\Column(type: 'simple_time')] public SimpleTime $mutableShiftStart; }
A warning about object comparison
PHP will let you compare the Simple(Date|Time)(Immutable|) classes with native \DateTimeInterface objects, but doing so is risky and discouraged. All classes offer a custom isSame, isBefore and isAfter methods that should be used for comparison - they will ignore the time zone in the object you're comparing the other side with.
janklan/simple-date-time provides a PHPStan rule that you should add to your PHPStan configuration to get warnings about unsafe comparisons.
Contributing
PR are welcome. All you need to do to get started is to check out the repository, run composer install and you're done.
Several shorthand commands are configured in Composer: cs, phpstan, rector, test, and preflight. See composer.json to find out what they do.
Run composer preflight at least at the end of your work, before you create a pull request.
Testing
The test suite is split into two categories: Unit and Functional tests.
Running all tests
By default, PHPUnit runs both unit and functional tests:
./vendor/bin/paratest
Note: Functional tests require database containers to be running (see below).
Unit Tests
Unit tests verify type conversion logic without requiring a database connection:
composer test:unit
Functional Tests
Functional tests verify actual database round-trips (PHP -> DB -> PHP) against real PostgreSQL and MySQL databases.
Starting the databases
Start the Docker containers before running functional tests:
docker compose up -d
This starts:
- PostgreSQL on port 15432 (default version: 18)
- MySQL on port 13306 (default version: 9)
Running functional tests
Run all functional tests:
composer test:functional
Run only PostgreSQL tests:
composer test:functional -- --group postgres
Run only MySQL tests:
composer test:functional -- --group mysql
Stopping the databases
docker compose down
Code Coverage
Generate coverage reports in ./coverage:
composer test:cc
License
MIT
janklan/simple-date-time-for-doctrine 适用场景与选型建议
janklan/simple-date-time-for-doctrine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「dbal」 「time」 「date」 「doctrine」 「types」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 janklan/simple-date-time-for-doctrine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 janklan/simple-date-time-for-doctrine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 janklan/simple-date-time-for-doctrine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Tools to convert between .NET and PHP date formats
Store your language lines in the database, yaml or other sources
Adds the EDTF data type to Wikibase
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A custom Doctine DBAL type to use PHP DateTime objects set to the system's default timezone.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-08