jennoramcaj / laravel-attributes
Rinvex Attributes 是一个强大、智能且集成的实体-属性-值模型(EAV)实现,用于 Laravel Eloquent。它能够轻松地将实体属性隐式地作为关系管理。它利用 Laravel Eloquent 的强大功能,实现了平稳且无缝的集成。
Requires
- php: ^8.0.0
- illuminate/console: ^9.0.0
- illuminate/database: ^9.0.0
- illuminate/support: ^9.0.0
- jennoramcaj/laravel-support: ^6.2.1
- jeremeamia/superclosure: ^2.4.0
- spatie/eloquent-sortable: ^4.0.0
- spatie/laravel-sluggable: ^3.3.0
- spatie/laravel-translatable: ^5.2.0
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.31.0
- illuminate/container: ^9.0.0
- phpunit/phpunit: ^9.5.0
This package is auto-updated.
Last update: 2024-09-06 15:00:41 UTC
README
⚠️ 此包已废弃且不再维护。未建议替代包。 ⚠️
Rinvex Attributes 是一个强大、智能且集成的实体-属性-值模型(EAV)实现,用于 Laravel Eloquent。它能够轻松地将实体属性隐式地作为关系管理。它利用 Laravel Eloquent 的强大功能,实现了平稳且无缝的集成。
鸣谢通知
此包是基于 IsraelOrtuno 的出色 EAV 包 重写的分支,原始信用归他所有。它已被广泛重写,与我们的基本概念相同,我们认为它本质上很好。这个分支的主要区别包括
- 利用 rinvex/laravel-cacheable 大幅提升了性能
- 使用它序列化和反序列化实体及其关系
- 在不增加框架无关的复杂性开销的情况下集成 Laravel
- 属性可以通过枢纽表附加到零个、一个或多个实体
- 属性可以排序、生成短链接、翻译、分组,并且最令人兴奋的是可缓存
- 实体属性被当作正常的 属性 以尽可能的 Eloquent 方式处理
- 实体属性也被当作正常的 关系 以尽可能的 Eloquent 方式处理
目录
简介
基础
序言
EAV 定义来自 维基百科
实体-属性-值模型(EAV)是一种数据模型,以空间高效的方式编码实体,其中可用于描述实体的属性(属性、参数)的数量可能很大,但实际上应用到特定实体的数量相对较小。
实体
实体表示需要动态扩展其属性的实时模型。例如:如 Product
、Customer
或 Company
这样的模型可能是实体。
在这种情况下,实体将由 Eloquent 模型表示。
属性
属性充当我们想要添加到实体中的“列”。属性获得一个像 price
、cities
或 colors
这样的短链接以进行识别,并将其附加到实体。它还将非常紧密地与数据类型实例配合,在写入或从数据库读取时转换或格式化其值。
此属性还将负责定义一些默认行为,如数据验证或默认值。
值
此功能负责存储与特定属性和实体实例(行)相关的数据值。
在 Rinvex Attributes 实现 中,一个值实例将代表与特定实体实例相关的属性内容。
根据数据类型,值存储在不同的表中。字符串值将存储在默认称为 attribute_varchar_values
的表中,而整数值将使用 attribute_integer_values
,依此类推。这两个表的列完全相同,只是 content
列的数据类型根据存储的数据类型进行适配。
性能损失
EAV 模型因其性能不佳而闻名。与查询任何其他水平结构的成本相比,它在查询数据方面的复杂性也很有名。这种范式在很多文章中被标记为反模式,并且关于是否应该使用它的争论很多。
由于我们将实体、属性和值存储在不同的表中,因此执行任何操作都需要进行多次查询。这意味着如果为一个实体注册了 4 个属性,则该包至少执行 5 次查询。
select * from `companies` select * from `attribute_varchar_values` where `attribute_id` = '1' and `attribute_varchar_values`.`entity_id` in ('1', '2', '3', '4', '5') and `eav_attribute_varchar_values`.`entity_type` = 'App\Models\Company' select * from `attribute_varchar_values` where `attribute_id` = '2' and `attribute_varchar_values`.`entity_id` in ('1', '2', '3', '4', '5') and `eav_attribute_varchar_values`.`entity_type` = 'App\Models\Company' select * from `attribute_varchar_values` where `attribute_id` = '3' and `attribute_varchar_values`.`entity_id` in ('1', '2', '3', '4', '5') and `eav_attribute_varchar_values`.`entity_type` = 'App\Models\Company' select * from `attribute_varchar_values` where `attribute_id` = '4' and `attribute_varchar_values`.`entity_id` in ('1', '2', '3', '4', '5') and `eav_attribute_varchar_values`.`entity_type` = 'App\Models\Company'
但是,有好消息! Rinvex Attributes 利用 Rinvex Cacheable 透明地缓存模型结果,可能将这些查询减少到仅一个或甚至零个查询!是的,这是可能的,并且默认已实现!!
获得的灵活性
然而,尽管存在性能问题,EAV 提供了非常高的灵活性。它允许我们拥有动态属性,可以在任何时间添加/删除,而不会影响数据库结构。它还帮助处理主要用于存储 NULL
值的列。
考虑到您接受了 EAV 带来的性能不足,该包的开发考虑到了灵活性,这样至少可以解决性能问题。可以通过在单个查询中加载与实体相关的所有值,并使用一些 PHP 逻辑将它们组织成关系来提高性能,但决定不这样做,以使数据库查询更加灵活。
如以下所述,此包将实体值加载得就像它们是自定义 Eloquent 关系一样。这就是为什么我们可以像查询常规 Eloquent 关系一样轻松查询它们的原因。
将值作为关系加载将使我们能够仅加载我们可能需要用于特定情况的值,而其他一些值则未加载。它还将使我们能够使用强大的 Eloquent 查询关系工具,这样我们就可以根据直接应用于值内容的条件轻松过滤从数据库中检索的实体。
更多技术细节
Rinvex\Attributes\Traits\Attributable
这个特质是最重要的,让其他类协同工作。
它负责处理实体内部的交互。此特质执行 EAV 属性的 set
和 get
操作,调用 RelationBuilder
类,向 $entityAttributeRelations
数组添加关系方法。这些关系可以像往常一样调用,因为我们正在覆盖魔法方法 __call
以查找这些调用。它负责设置保存和删除的事件监听器,添加全局范围,并获取与该实体相关的属性。
尝试访问实体属性时,如果它对应于 EAV 属性,则此特质包含提供其值的逻辑、创建新值实例、更新集合或任何其他 set/get 交互。
在读取值时,没有太多要检查的事情。如果值存在,我们只需格式化并提供它,否则我们将返回 null 或空集合。
在设置值时,事情变得稍微复杂一些。在设置值时,我们需要考虑三件事情。
- 设置一个数据库中不存在的单个值,因此我们必须创建新的模型实例并将其与属性和实体关联。
- 更新现有单个值模型(数据库行)的内容。
- 用新的值集合替换现有的(或空的)值集合,因此我们必须删除之前存储的值(从数据库中删除)。
它还覆盖了一些实体方法,例如 bootIfNotBooted
、relationsToArray
、setRelation
、getRelationValue
,以便以各种可能的方式提供与 Eloquent 模型的平滑无缝集成。它将所有东西连接在一起。
// To build entity relations for every instance bootIfNotBooted(); // To include attributes as relations when converting to array/json relationsToArray(); // To link entity & attribute to value collections (multi-valued attributes) setRelation() // To let Eloquent use our attribute relations as part of the model getRelationValue()
Rinvex\Attributes\Support\RelationBuilder
此类根据它们的类型创建 Eloquent 与属性值的关联。如果是多值,则提供 hasMany
关联;否则仅提供一个 hasOne
。此类创建返回此类关联的闭包,并可以直接从实体模型中调用。这些闭包存储在 \Rinvex\Attributes\Traits\Attributable
特性中的 $entityAttributeRelations
属性中。
安装
-
使用 composer 安装此包
composer require rinvex/laravel-attributes
-
发布资源(迁移和配置文件)
php artisan rinvex:publish:attributes
-
执行以下命令以执行迁移
php artisan rinvex:migrate:attributes
-
完成!
使用
将 EAV 添加到 Eloquent 模型
Rinvex Attributes 是专门为 Eloquent 制作的,并且非常重视简洁性,就像 Laravel 的其他任何相关方面一样。要向 Eloquent 模型添加 EAV 功能,只需像这样使用 \Rinvex\Attributes\Traits\Attributable
特性
class Company extends Model { use \Rinvex\Attributes\Traits\Attributable; }
就这样,我们只需要在我们的 Eloquent 模型中包含该特性!
核心类型
\Rinvex\Attributes\Models\Type\Text::class \Rinvex\Attributes\Models\Type\Boolean::class \Rinvex\Attributes\Models\Type\Integer::class \Rinvex\Attributes\Models\Type\Varchar::class \Rinvex\Attributes\Models\Type\Datetime::class
注册你的类型
Rinvex Attributes 默认不注册任何类型,因为这被视为实现细节,所以您需要注册上述核心类型,或扩展它们并仅注册自定义类型。
use Rinvex\Attributes\Models\Attribute; Attribute::typeMap([ 'varchar' => Rinvex\Attributes\Models\Type\Varchar::class, // ... 'custom' => \Path\To\Your\Type::class, ]);
注意:虽然您可以从应用程序的任何位置注册自定义类型,但建议在服务提供者的
boot
方法中这样做。
注册你的实体
// Push your entity fully qualified namespace app('rinvex.attributes.entities')->push(\Path\To\Your\Entity::class); // Or push the morph class alias if any app('rinvex.attributes.entities')->push('entity');
您可以在应用程序的任何地方调用 'rinvex.attributes.entities'
服务,在请求生命周期的任何时候(建议在服务提供者的 boot
方法内部)。它是一个单例对象,包含一个纯 Laravel 集合。
创建新属性
像任何正常的 Eloquent 模型一样,您可以按以下方式创建属性
app('rinvex.attributes.attribute')->create([ 'slug' => 'size', 'type' => 'varchar', 'name' => 'Product Size', 'entities' => ['App\Models\Company', 'App\Models\Product'], ]);
管理属性实体
每当您需要获取与特定属性关联的实体时,您可以这样做
$attribute = app('rinvex.attributes.attribute')->find(1); // Get attribute entities collection $attribute->entities // Get attribute entities query builder $attribute->entities(); // Delete attached attribute entities $attribute->entities()->delete(); // Attach attribute entities $attribute->entities()->createMany([ [...], [...], [...], ]); // Alternative way of attaching attribute entities $attribute->fill([ 'entities' => ['App\Models\Company', 'App\Models\Product'], ])->save(); // Get all attribute values of type varchar $values = $attribute->values(\Rinvex\Attributes\Models\Type\Varchar::class)->get();
分配值
您可以将新创建的自定义属性视为普通属性,是的!所有属性都是平等的!😄
您需要证据吗?好吧,看看以下示例,我们假设 price
是我们刚刚创建并链接到我们的 \App\Models\Product
模型的自定义属性
// Single value assignment $product = \App\Models\Product::find(1); $product->price = 123; $product->save(); // Mass assignment $product = \App\Models\Product::find(1); $product->fill(['price' => 123])->save();
是的,就是这样。很简单!您可以像处理普通属性一样处理自定义属性,没有区别。您所知道的所有关于 eloquent 的好东西都适用于这里,无论是更新单个字段、批量分配、创建还是更新,它只是工作!
Rinvex\Attributes\Support\ValueCollection
Rinvex Attributes 允许您注册多值属性。为了使操作集合更容易,我们包括了一个新的集合类型,它仅扩展 Illuminate\Database\Eloquent\Collection
并提供一些额外功能。此类允许我们从属性中添加和删除值。它基本上允许用户无需担心创建值模型实例就玩转集合类。一些代码将帮助这里
// This is how it works $entity->cities->add('Alexandria'); // And this is what you would have to do without this collection: $value = new Varchar(['content' => 'Alexandria', 'attribute_id' => 1, 'entity_type' => 'App\Models\Company', 'entity_id' => 1]); $entity->cities->push($value); // You could also pass an array $entity->cities->add(['Alexandria', 'Cairo']);
集合可能会得到改进并添加更多功能,但现阶段已经足够了。基于值的模型替换了 Eloquent 方法 newCollection
,以便在处理多值属性时返回此类集合。
查询模型
Rinvex 属性 尝试以与 Eloquent 通常方式相同的方式完成所有操作。在加载模型时,它会内部为每个实体属性创建一个常规关系。这意味着我们可以像查询 Eloquent 关系那样,通过我们注册的属性值进行查询过滤。
// Cities is an entity attribute $companies = Company::whereHas('Cities', function (\Illuminate\Database\Eloquent\Builder $builder) { $builder->where('content', 'Alexandria'); })->get();
或者简单地使用以下内置查询作用域
$companies = Company::hasAttribute('Cities', 'Alexandria')->get();
当然,您可以像正常 Eloquent 属性那样检索实体属性,或者作为原始关系
$company = Company::find(1); // Get entity attributes $company->cities; // Get entity raw relation $company->cities();
预加载
Rinvex 属性 考虑到强大的 Eloquent 预加载系统。在访问 Eloquent 模型中的实体属性时,它将与 Eloquent 一样在需要时进行加载。然而,我们可以使用 Eloquent 预加载来与 Rinvex 属性 一起工作,以提高性能并避免 n+1 查询问题。
Rinvex 属性 为加载所有已注册的属性保留了一个特殊的关系名称。这个关系被称为 eav
。当使用 eav
来加载值时,它将加载与我们所操作的实体相关的所有属性,就像您明确包含在 $with
模型属性中的所有关系一样。
延迟预加载
同样,就像任何常规 Eloquent 关系一样,我们可以决定何时加载我们的属性。就像您通常加载关系一样进行操作
$company->load('eav'); $company->load('cities', 'colors');
使用 $with 进行自动加载
Eloquent 内置了一个 $with
,它接受一个数组,指定了应该预加载的关系。我们也可以使用它
namespace App\Models; use Rinvex\Attributes\Traits\Attributable; class Company extends Model { use Attributable; // Eager loading all the registered attributes protected $with = ['eav']; // Or just load a few of them protected $with = ['cities', 'colors']; }
注意: 如果您的模型预加载了
eav
关系,并且它已被排队用于发送通知,这可能会导致一些问题,因为eav
关系是通过全局作用域评估的,而用于排队通知的SerializesAndRestoresModelIdentifiers
特性在反序列化排队模型时没有全局作用域,因此您将得到 "Call to undefined relationship [eav] on model [App\Models\Company]" 异常。
变更日志
有关项目的完整历史记录,请参阅变更日志。
支持
以下支持渠道随时可供您使用
贡献 & 协议
感谢您考虑为这个项目做出贡献!贡献指南可以在 CONTRIBUTING.md 中找到。
非常欢迎错误报告、功能请求和拉取请求。
安全漏洞
如果您在此项目中发现了安全漏洞,请发送电子邮件至 help@rinvex.com。所有安全漏洞都将得到及时处理。
关于 Rinvex
Rinvex 是一家成立于2016年6月的开罗,埃及的软件解决方案初创公司,专注于为中小企业提供集成企业解决方案。我们相信,我们的动力——价值、触达和影响——是我们与众不同的地方,通过软件的力量释放我们哲学的无限可能性。我们喜欢称之为“生命之速的创新”。这就是我们为推动人类进步所做的一份贡献。
许可
本软件在 MIT 许可证 (MIT) 下发布。
© 2016-2022 Rinvex LLC,部分权利保留。