artistan/zero-nullable-dates 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

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 NULL RETURN IT HERE
  • 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

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 artistan/zero-nullable-dates 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2018-08-13