testinstance/laravel-model-profiles

该软件包已被废弃且不再维护。未建议替代软件包。

Laravel 模型配置 - 基于垂直模型的键值数据扩展

1.0 2021-12-24 19:30 UTC

This package is auto-updated.

Last update: 2024-02-25 00:14:23 UTC


README

testinstancebanner.png

Total Downloads Latest Version License

Laravel 模型配置

Laravel 模型配置允许扩展传统规范化形式,通过表示具有数据类型和关系键的值而不是额外的表和基于键的关系的行来完成扩展。

注意 - 仅兼容 LARAVEL 框架

为什么?

配置表允许在不添加新列到原始模型表的情况下,根据模型扩展或减少数据点。例如,一个有400列的数据表可以压缩成一个只有1列(主键id)的表。

设置

  • (版本 1) 您的模型必须位于 App/Models

  • (从版本 2 开始) [WIP] 在 app/config/profiles.php 文件中设置您的模型路径和迁移路径。这些告诉 Laravel 模型配置在哪里通过创建注入新的配置模型和迁移。默认为 App/Modelsdatabase/migrations

使用

安装

composer require testinstance/laravel-model-profiles

我们将使用 'User.php' 模型作为此使用指南的示例。

创建模型配置

为了使用 Laravel 模型配置,您必须在所选模型类上使用 'use HasProfile' 特性。'use TestInstance\LaravelModelProfiles\Traits\HasProfile;'。

运行 php artisan make:profile ModelScope\ModelClassName

  • 示例:php artisan make:profile User\User。这将为 User.php 模型创建配置。

您将看到创建了两个新模型和一个迁移文件

  • App/Models/User/UserProfile.php
  • App/Models/User/UserProfileKey.php
  • database/migrations/{timestamp}_user_profile.php

运行 php artisan migrate

您将看到创建的 user_profiles 表包含以下字段

  • id
  • user_id
  • user_profile_key_id
  • value
  • created_at
  • updated_at
  • deleted_at
  • deleted_at_unique

您将看到创建的 user_profile_keys 表包含以下字段

  • id
  • name
  • type
  • created_at
  • updated_at

交互

通过配置值检索

//'color' is the name of the profileKey related to the model - 'user_profile_keys.name'
//'blue' is the value of the profile related to the model - 'user_profiles.value'
$users = User::whereProfile('color', 'blue')->get();

$users 将是具有 profile_key 名称 color 的配置值为 blueusers 集合

whereProfilewhereProfileIn 是直接传递到 Laravel 的原生 wherewhereIn,因此可以如此使用。包括作为数组搜索多个参数:whereProfile(['color' => 'blue', 'anotherProfileKey' => 'anotherProfileValue'])

profileKey 类型将决定它相关联的数据类型,并将以此类型进行转换。可用的类型在 Laravel 文档中列出,请参阅此处。在 profile 中的模型转换是一个扩展,将在完全发布后进行文档记录。

保存配置文件值

要保存配置文件值,可以使用以下格式

$user->color = 'blue';
$user->save();

预加载

每个具有配置文件的模型都会预加载其配置文件。可以通过与模型属性相同的方式访问配置文件属性。

  • 示例:`$user->color` 将返回 `blue`,即使 'color' 属性不是用户表中的列。

说明

  • 批量赋值

带有配置文件属性加载的模型更新将不受配置文件的影响。

  • 模型转换

将模型配置文件类型转换为其他模型是[进行中]

  • 单元测试

存在配置文件的单元测试,但特定于模型的测试不存在。这些是[进行中]。可以在当前版本中手动创建这些测试。

依赖

  • Laravel 框架:"^8.75"(推荐)
  • PHP:"^8.0"

安全

请通过 developers@testinstance.co.uk 通知我们任何安全问题和担忧。

许可

Laravel 模型配置文件是开源软件,许可协议为MIT 许可证

作者

Ballard-dev KieranFYI