artistan/zero-nullable-dates
Composer 安装命令:
composer require artistan/zero-nullable-dates
包简介
Allows having zero date models in laravel
README 文档
README
Laravel Models with nullable carbon dates, this also allow for returning a date object. When the value is null/zeros that will format to ''.
example
// my_date on new model is null... // this will just be null value // so you can use the null coalesce operator to set a string echo $user->my_date->format('Y-m-d H:i:s') ?? 'Set a Date';
This is useful for old databases that do not have NO_ZERO_IN_DATE, NO_ZERO_DATE so they may have 0000-00-00 type dates.
Add ZNDTrait and a couple arrays to define nullable dates to allow for zero and nullable dates.
You may also set null_all_dates on your model to set all to nullable without the arrays
Ultimately you can just set them all to $nullable, the addition of $zero_datetime and $zero_date are for testing that the database is updating as expected.
use Artistan\ZeroNullDates\ZNDTrait; /** * @see tests/Database/ZeroNullableUser.php */ class ZeroNullableUser extends Authenticatable { use Notifiable; use ZNDTrait; /** * The attributes that are zero-datetimes * * @var array */ public static $zero_datetime = [ 'created_date', ]; /** * The attributes that are zero-dates * * @var array */ public static $zero_date = [ 'created_date_time', 'created_date_time_zoned', ]; /** * The attributes that are nullable dates * * @var array */ public static $nullable = [ 'created_date_time_null', 'created_date_time_zoned_null', 'created_date_null', 'created_at', 'updated_at', ]; }
The unit tests are dynamic and test 288 assertions
test for all combinations of what things are set for date columns.... all combinations of mutators, castings, and dates
| mutator | cast | date |
|---|---|---|
| x | ||
| x | ||
| x | ||
| x | x | |
| x | x | |
| x | x | |
| x | x | x |
notes on current laravel functionality dealing with toArray, attribute accessors, and dates
#toArray vs accessor
if you use $model->{camel_key} (getAttributeValue), then it will
- model->{key} > getAttribute(key) > getAttributeFromArray(key)
- get the original attribute
- mutate : if hasGetMutator > mutateAttribute
- this is the accessor get{CasedKey}Attribute
- mutate it if there is a mutator RETURN IT HERE
- cast : if hasCast > castAttribute
- cast it if there is a cast method RETURN IT HERE
- date format : if matches getDates > asDateTime
- finaly make a date (carbon) object from it
IF NOT NULLRETURN IT HERE
- finaly make a date (carbon) object from it
- return value
note: the appends variables ARE mutated attributes that do not exist in the model attributes...
if you get attributes via $model->toArray then it will
- addDateAttributesToArray via getArrayableAttributes
- so this is cast to a string AFTER carbon dating, no other processing will be applied
- addMutatedAttributesToArray via getMutatedAttributes
- so AFTER dates transformed from carbon, then it will try mutating the formated data
- can mutate the already "date formated" data since it passes that value to the
mutateAttribute
- addCastAttributesToArray
- this will cast a value to whatever type is specified
- if date it will serialize it to the format specified
- note, this is AFTER dates have already been carbon dated and formated by mutator
- getArrayableAppends
- this will add any mutators that are not directly related to a model attibutue
date mutators.
these are both mutators and accessors, or mutates in and out
- get from db sends us a carbon date
data analysis
when date keys are set for dates that allow ZERO dates the formatting for 000-00-00 or variations of that fail.
-0001-11-30 00:00:00 is the default result...
ZNDCarbon
using NULL as the data point when it is a zero nullable date
private static $empty_date = ['0', 0, false, '', '0000-00-00', '0000-00-00 00:00:00'];
mysql mode
mysql
SELECT @@sql_mode
homestead box setup for testing
vim /etc/mysql/mysql.conf.d/mysqld.cnf
sql_mode="ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
notes on dev package setup
- example package https://medium.com/@lasselehtinen/getting-started-on-laravel-package-development-a62110c58ba1
- package setup https://stackoverflow.com/a/44023017/372215
- test setup https://github.com/orchestral/testbench
- .env file in tests directory for configuration of your tests
ln -s vendor/laravel/laravel/configin tests directory to allow access to config files- laravel does not have a seperate "config" path from the base path, damn it.
artistan/zero-nullable-dates 适用场景与选型建议
artistan/zero-nullable-dates 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 08 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「model」 「laravel」 「dates」 「zero」 「nullable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 artistan/zero-nullable-dates 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 artistan/zero-nullable-dates 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 artistan/zero-nullable-dates 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Laravel / Lumen library that allows to change dates in db
Provide an easy class easy way to get the time difference between two dates, with an extra bonus trait for eloquent
Laravel 5 - Repositories to the database layer
A library to make it easier to work with dates/times.
统计信息
- 总下载量: 41
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2018-08-13