mralston/laravel-eav
Composer 安装命令:
composer require mralston/laravel-eav
包简介
Implements the Entity-Attribute-Value pattern to supplement Eloquent with adhoc attributes.
README 文档
README
Introduction
Entity Attribute Value package for Laravel.
EAV Definition From Wikipedia:
Entity–attribute–value model (EAV) is a data model to encode, in a space-efficient manner, entities where the number of attributes (properties, parameters) that can be used to describe them is potentially vast, but the number that will actually apply to a given entity is relatively modest.
TL;DR this package allows you to work with attributes on your models which aren't defined in the underlying database schema.
But why though?
Your scientists were so preoccupied with whether or not they could that they didn't stop to think if they should.
-- Dr. Ian Malcolm (Jeff Goldblum), Jurassic Park
- Yes, there are another packages which already provide similar functionality.
- Yes, EAV is sometimes considered an anti-pattern due to performance issues.
- Yes, this might be considered reinventing the NoSQL wheel.
- Yes, I did it anyway. 🤷🏻♂️
Truth be told, this started as a proof-of-concept based on an idea I had, aimed at improving a messy pre-existing implementation in several projects which I am involved in maintaining.
The EAV implementations which I've seen thus far have a separate database record for each EAV attribute. I believe that the bulk of the performance problems comes from the large amounts of additional database traffic as a result.
My concept is to place all of the EAV data into a JSON field in a single database record, which can be fetched just once per model instance.
Inspiration for using a JSON field came from Aaron Francis @ PlanetScale, so kudos to him. Also thanks to Aaron for the get-out-of-jail-free card when it comes to the 'you should just use a NoSQL database' argument.
Installation
Install the package using Composer:
composer require mralston/laravel-eav
Config
This package has no config, it doesn't need it. Yay!
Migrations
After installing, you need to migrate the package's single database table.
php artisan migrate
Usage
This package is designed to be completely transparent. You can read and write to model attributes just as you normally would.
Any model which you would like to use EAV will need to use the HasEntityAttributeValues trait:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Mralston\Eav\Traits\HasEntityAttributeValues; class MyModel extends Model { use HasEntityAttributeValues; ... }
Then all you need to do is set attributes on your model as you normally would:
$myModel->myField = 'test'; dump($myModel->myField); // test
If the attribute you set is defined as a field on the underlying table, it will be stored there as normal. Otherwise, it will be stored as an EAV attribute.
Other methods are also available which allow access to EAV data.
The eav method operates as a combined accessor and mutator, depending on whether it is passed the optional $value argument.
$myModel->eav('myField', 'test'); dump($myModel->eav('myField')); // test
The underlying EAV store is assessable using the entityAttributeStore relationship. It provides get, set and unset methods:
$myModel->entityAttributeStore->set('myField', 'test'); $myModel->entityAttributeStore->get('myField'); // test $myModel->entityAttributeStore->unset('myField'); $myModel->entityAttributeStore->get('myField'); // null
A note on Persistence
EAV data is persisted to the database when you save the associated model. Until that time, any changes you make exist only on the model in memory - just like standard Eloquent attributes.
// Load model from database and set an EAV attribute $myModel = MyModel::find(1); $myModel->myField = 'test'; dump($myModel->myField); // test // Load a second copy of the model and attempt to // retrieve the same EAV attribute. // It fails because it hasn't been saved yet $myModel2 = MyModel::find(1); dump($myModel2->myField); // null // Save the first model and its EAV attributes $myModel->save(); // Load another copy of the model from the database. // This one will have the EAV data $myModel3 = MyModel::find(1); dump($myModel3->myField); // test
Likewise, if you instantiate several individual models of the same database record, each model instance will have its own distinct copy of the EAV attributes. You can modify an EAV attribute on one model instance and the others will remain unaffected - just like standard Eloquent attributes.
$myModel = MyModel::find(1); $myModel->myField = 'foo'; $myModel2 = MyModel::find(1); $myModel->myField = 'bar'; dump($myModel->myField); // foo dump($myModel2->myField); // bar
Security Vulnerabilities
Please e-mail security vulnerabilities directly to me.
Licence
Laravel EAV is open-sourced software licenced under the MIT license.
mralston/laravel-eav 适用场景与选型建议
mralston/laravel-eav 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 11 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「entity attribute value」 「mralston」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mralston/laravel-eav 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mralston/laravel-eav 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mralston/laravel-eav 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Value Objects Library
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
generates php entities from openapi spec
DCSDynamicDiscriminatorMapBundle simplifies the use of Doctrine Single Table Inheritance mapping strategy in Symfony2.
swoft 2.0 entity creater
PhpPlaisio: Core Entity Lock
统计信息
- 总下载量: 9
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-05