amethyst/relation-schema 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

amethyst/relation-schema

Composer 安装命令:

composer require amethyst/relation-schema

包简介

README 文档

README

Action Status Amethyst

Create your own custom relations between Eloquent Models without altering the code.

Requirements

  • PHP from 8.2 and later

Installation

You can install it via Composer by typing the following command:

composer require amethyst/relation-schema

The package will automatically register itself.

Initialization

Add app('amethyst.relation-schema')->boot(); in any ServiceProvider in the method boot

Usage

A simple usage looks like this

use Symfony\Component\Yaml\Yaml;

app('amethyst')->get('relation-schema')->createOrFail([
    'name'    => 'parent',
    'type'    => 'BelongsTo',
    'data'    => 'foo',
    'payload' => Yaml::dump([
        'target' => 'bar',
    ]),
]);

Here's a list of all attributes with a brief explanation:

  • name: The name of the relation. Can be only alphanumeric and it must start with a letter
  • description: An optional field that will help to describe the relation
  • type: The type of the relation. Values: BelongsTo, HasMany, HasOne, MorphTo, MorphToMany, MorphToOne, MorphMany
  • data: The name of the data this relation start with. If a Book is related to an Author through the relation BelongsTo named author, then book is the data
  • payload: A sets of information used to define better the relation, this changes based on the type of the relation. For example in a BelongsTo relation you'll neet a target, in the previous example Author is the target.

Whenever refering to any model, we will call it by name. For e.g. \App\Models\Customer will be customer. More info

Keep in mind that this is an Amethyst Package, if you wish to see the full list of available features and customization please check core

Relationships

As said before, based on the type attribute, payload must have a different sets of value. It uses Yaml for the serialization, so for all examples we will use a Symfony\Component\Yaml\Yaml to convert from array to Yaml.

Most of relation uses the laravel-defined convention of names, such as foreignKey or ownerKey.

BelongsTo

Starting with the most simple relation, BelongsTo. It requires only the target parameter that indicate to which Model we are relating.

You can also set the foreignKey option.

So for example this new record

use Symfony\Component\Yaml\Yaml;

app('amethyst')->get('relation-schema')->createOrFail([
    'name'    => 'parent',
    'type'    => 'BelongsTo',
    'data'    => 'foo',
    'payload' => Yaml::dump([
        'target' => 'bar',
        'foreignKey' => 'foreign_key'
    ])
]);

is the exact same thing like this

use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Foo
{
	public function parent(): BelongsTo
	{
		return $this->belongsTo(Bar::class, 'foreign_key');
	}
}

HasMany and HasOne

The inverse relationship of BelongsTo. They behave exactly the same.

Beside the target you can set foreignKey and localKey.

Now let's look an example of the same relationship in BelongsTo but inverted

use Symfony\Component\Yaml\Yaml;

app('amethyst')->get('relation-schema')->createOrFail([
    'name'    => 'children',
    'type'    => 'HasMany',
    'data'    => 'bar',
    'payload' => Yaml::dump([
        'target' => 'foo',
        'foreignKey' => 'foreign_key',
        'localKey' => 'local_key'
    ]),
]);

Will result in:

use Illuminate\Database\Eloquent\Relations\HasMany;

class Bar
{
	public function children(): HasMany
	{
		return $this->belongsTo(Foo::class, 'foreign_key', 'local_key');
	}
}

But wait, there's more! You can create relations with a custom subfilter. Say for example that having a customer and invoice you want to create a relation that retrieve all invoices with status:paid from your customer.

You can add another parameter called filter in the payload.

So having something like this: filter: "status eq 'paid'". You can also use auto joins here, so for e.g. a query like: only the invoice that contains Maintenance in the name, will result in filter: "status eq 'paid' and items.name ct 'Maintenance'".

Limitation: You cannot insert in the filter the same relation you're currently defining

ManyToMany and BelongsToMany

Not yet implemented.

MorphTo

Similar toBelongsTo it doesn't require a target, instead it requires a foreignKey that in this case means the name of the field

MorphToMany and MorphToOne

WIP

MorphMany

WIP

Api

There are no additional routes in this package, only the default provided by the core.

Testing

  • Clone this repository
  • Copy the default phpunit.xml.dist to phpunit.xml
  • Change the environment variables as you see fit
  • Launch ./vendor/bin/phpunit

amethyst/relation-schema 适用场景与选型建议

amethyst/relation-schema 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 432 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 10 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「amethyst」 「relation-schema」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 amethyst/relation-schema 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 amethyst/relation-schema 我们能提供哪些服务?
定制开发 / 二次开发

基于 amethyst/relation-schema 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

与 amethyst/relation-schema 相关的其它包

同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:

统计信息

  • 总下载量: 432
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 5
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-10-18