friendsofhyperf/compoships
Composer 安装命令:
composer require friendsofhyperf/compoships
包简介
Hyperf relationships with support for composite/multiple keys.
README 文档
README
Compoships offers the ability to specify relationships based on two (or more) columns in Hyperf's Model ORM. The need to match multiple columns in the definition of an Eloquent relationship often arises when working with third party or pre existing schema/database.
The problem
Eloquent doesn't support composite keys. As a consequence, there is no way to define a relationship from one model to another by matching more than one column. Trying to use where clauses (like in the example below) won't work when eager loading the relationship because at the time the relationship is processed $this->team_id is null.
namespace App; use Hyperf\Database\Model\Model; class User extends Model { public function tasks() { //WON'T WORK WITH EAGER LOADING!!! return $this->hasMany(Task::class)->where('team_id', $this->team_id); } }
Installation
The recommended way to install Compoships is through Composer
composer require friendsofhyperf/compoships
Usage
Using the FriendsOfHyperf\Compoships\Database\Eloquent\Model class
Simply make your model class derive from the FriendsOfHyperf\Compoships\Database\Eloquent\Model base class. The FriendsOfHyperf\Compoships\Database\Eloquent\Model extends the Eloquent base class without changing its core functionality.
Using the FriendsOfHyperf\Compoships\Compoships trait
If for some reasons you can't derive your models from FriendsOfHyperf\Compoships\Database\Eloquent\Model, you may take advantage of the FriendsOfHyperf\Compoships\Compoships trait. Simply use the trait in your models.
Note: To define a multi-columns relationship from a model A to another model B, both models must either extend FriendsOfHyperf\Compoships\Database\Eloquent\Model or use the FriendsOfHyperf\Compoships\Compoships trait
Syntax
... and now we can define a relationship from a model A to another model B by matching two or more columns (by passing an array of columns instead of a string).
namespace App; use Hyperf\Database\Model\Model; class A extends Model { use \FriendsOfHyperf\Compoships\Compoships; public function b() { return $this->hasMany('B', ['foreignKey1', 'foreignKey2'], ['localKey1', 'localKey2']); } }
We can use the same syntax to define the inverse of the relationship:
namespace App; use Hyperf\Database\Model\Model; class B extends Model { use \FriendsOfHyperf\Compoships\Compoships; public function a() { return $this->belongsTo('A', ['foreignKey1', 'foreignKey2'], ['ownerKey1', 'ownerKey2']); } }
Example
As an example, let's pretend we have a task list with categories, managed by several teams of users where:
- a task belongs to a category
- a task is assigned to a team
- a team has many users
- a user belongs to one team
- a user is responsible for one category of tasks
The user responsible for a particular task is the user currently in charge for the category inside the team.
namespace App; use Hyperf\Database\Model\Model; class User extends Model { use \FriendsOfHyperf\Compoships\Compoships; public function tasks() { return $this->hasMany(Task::class, ['team_id', 'category_id'], ['team_id', 'category_id']); } }
Again, same syntax to define the inverse of the relationship:
namespace App; use Hyperf\Database\Model\Model; class Task extends Model { use \FriendsOfHyperf\Compoships\Compoships; public function user() { return $this->belongsTo(User::class, ['team_id', 'category_id'], ['team_id', 'category_id']); } }
Contact
License
friendsofhyperf/compoships 适用场景与选型建议
friendsofhyperf/compoships 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.94k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 08 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「hyperf」 「Hyperf composite keys」 「Hyperf relationships」 「v3.2」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 friendsofhyperf/compoships 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 friendsofhyperf/compoships 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 friendsofhyperf/compoships 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Most advanced composite primary keys package with support of: binary columns, queueable, implicit route binding, relations
An async event for hyperf.
A simple interface for composite Flysystem adapters
Vault for private keys and secret data
php hyperf xxljob
Laravel SQLite Foreign Keys Support
统计信息
- 总下载量: 23.94k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-02