philippgrashoff/atkdatamodeltraits
Composer 安装命令:
composer require philippgrashoff/atkdatamodeltraits
包简介
A small collection of traits to be used with Atk4\Data\Model
README 文档
README
A small collection of traits to be used with Atk4\Data\Model.
CreatedDateAndLastUpdatedTrait
This trait can be used to add a created_date and/or a last_updated field to a model. The corresponding hooks are automatically added to save created_date on insert and last_updated on update.
class SomeModel extends Model { use CreatedDateAndLastUpdatedTrait; protected function init(): void { parent::init(); $this->addCreatedDateFieldAndHook(); $this->addLastUpdatedFieldAndHook(); } }
UniqueFieldTrait
This trait provides a function isFieldUnique() to check if the field's current value is unique among all records in persistence.
This functionality can also be achieved by adding a UNIQUE index to an SQL persistence. Having this functionality in the application layer
can be sensible to be independent of the persistence's features - or to avoid getting back an exception from the persistence if a non-unique value is being tried to save.
class SomeModel extends Model { use UniqueFieldTrait; protected function init(): void { parent::init(); $this->addField('unique_field'); } $this->onHook( Model::HOOK_BEFORE_SAVE, function(self $entity, bool $isUpdate) { while(!$entity->isFieldUnique('unique_field')) { //some function which recalculates field value. } } ); }
CryptIdTrait
This trait is used to generate cryptic IDs like D6f2-a395Jskv2. You can freely define the format the cryptic ID shall have.
The functionality can for example be used to create coupon codes or create unguessable, yet human-readable codes/identifiers.
The characters I,l,0 and O are removed as they can be easily be mistaken by humans.
To use this trait, you just need to add the cryptic ID field in Model::init() using addCryptIdFieldAndHooks and implement a custom method
generateCryptId() that returns a random string in the format of your choice:
class SomeModel extends Model { use CryptIdTrait; protected function init(): void { parent::init(); $this->addCryptIdFieldAndHooks('crypt_id'); } /** * Custom implementation if cryptic ID format, In this case XXXXX-XXXXX-XXXXX */ protected function generateCryptId(): string { $cryptId = ''; for ($i = 0; $i < 3; $i++) { if($i > 0) { $cryptId .= '-'; } for ($j = 0; $j < 5; $j++) { $cryptId .= $this->getRandomChar(); } } return $cryptId; } }
philippgrashoff/atkdatamodeltraits 适用场景与选型建议
philippgrashoff/atkdatamodeltraits 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.66k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 08 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 philippgrashoff/atkdatamodeltraits 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 philippgrashoff/atkdatamodeltraits 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2023-08-13