jsolam / laravel-attributes
Rinvex Attributes 是一个强大的、智能的、集成的实体-属性-值模型(EAV)实现,用于 Laravel Eloquent,它可以通过轻松地将实体属性作为关系隐式管理来有效地管理实体属性。它利用 Laravel Eloquent 的强大功能,实现平滑无缝的集成。
Requires
- php: ^7.2.0
- illuminate/console: ~5.8.0
- illuminate/database: ~5.8.0
- illuminate/support: ~5.8.0
- jeremeamia/superclosure: ^2.4.0
- rinvex/laravel-cacheable: ^2.0.0
- rinvex/laravel-support: ^2.0.0
- spatie/eloquent-sortable: ^3.4.0
- spatie/laravel-sluggable: ^2.1.0
- spatie/laravel-translatable: ^3.1.0
- watson/validating: ^3.2.0
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.26.0
- illuminate/container: ~5.8.0
- orchestra/testbench: ^3.6
- phpunit/phpunit: ^8.0.0
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 属性相对应,此特性包含提供其值的逻辑,创建新的值实例,更新集合或任何其他设置/获取交互。
读取值时没有太多要检查的,如果值存在,我们只需格式化和提供它,否则我们将返回 null 或空集合。
设置值时会更复杂一些。在设置值时,我们有 3 件事要考虑
- 设置一个数据库中不存在的单个值,因此我们必须创建新的模型实例并将其与属性和实体相关联。
- 更新现有单个值模型的内 容(数据库行)。
- 将现有的(或空的)值集合替换为新的,因此我们必须删除之前存储的值(从数据库中删除)。
它还覆盖了一些实体方法,例如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:migrate:attributes
-
可选如果您想更改配置
php artisan rinvex:publish:attributes
-
完成!
使用
将 EAV 添加到 Eloquent 模型
Rinvex Attributes专为Eloquent设计,简洁性被非常认真地对待,正如Laravel相关的其他方面一样。要将EAV功能添加到您的Eloquent模型,只需像这样使用\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, // ... '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 Collection。
创建新属性
就像任何正常的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('varchar')->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
并提供了一些额外功能。此类允许我们从属性中添加和删除值。它基本上是让用户玩集合类,而无需担心创建Value模型实例。以下是一些代码将有助于说明这一点
// 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']);
集合可能会得到改进并添加更多功能,但就目前而言已经足够了。基于值的模型用newCollection
方法替换了Eloquent方法,以便在处理多值属性时返回此类集合。
查询模型
Rinvex Attributes试图以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) 发布。
(c) 2016-2019 Rinvex LLC,部分权利保留。