定制 culturegr/custom-relation 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

culturegr/custom-relation

Composer 安装命令:

composer require culturegr/custom-relation

包简介

Easy implementation of custom Eloquent relations

README 文档

README

Latest Version on Packagist Total Downloads Github Actions

This package provides an easy way to implement custom relationships between Eloquent models

Installation

Via Composer:

$ composer require culturegr/custom-relation

In Laravel 5.5+, the package's service provider should be auto-discovered, so you won't need to register it. If for some reason you need to register it manually you can do so by adding it to the providers array in config/app.php:

'providers' => [
    // ...
    CultureGr\CustomRelation\CustomRelationServiceProvider::class,
],

Usage

Suppose we have a Laravel application that implements a simple ACL (Access Control List) layer: there are users that are assigned some roles, each of them consists of many permissions. A simplified version of the database structure could be the following:

Alt text

There is a User model that has a many-to-many relationship with a Role model, which in turn has a many-to-many relationship with a Permission model.

Now suppose that at some point, we need to access all permissions assigned to a specific user. Let's make this possible by creating a CustomRelation class that will be used to define the relationship between the User and the Permission models.

Creating a Custom Relation Class

A CustomRelation class should facilitate all required logic needed to join users and permissions tables, as well as to support relationship eager-loading. It can be created by running the make:relation Artisan command:

$ php artisan make:relation UserPermissionRelation

This will generate a new CustomRelation class named UserPermissionRelation inside app/Eloquent/CustomRelations directory with all required boilerplate:

<?php

namespace App\Eloquent\CustomRelations;

use CultureGr\CustomRelation\CustomRelation;
use Illuminate\Database\Eloquent\Collection;

class UserPermissionRelation extends CustomRelation
{
   /**
    * The Eloquent query builder instance.
    *
    * @var \Illuminate\Database\Eloquent\Builder
    */
   protected $query;


   /**
    * The parent model instance.
    *
    * @var \Illuminate\Database\Eloquent\Model
    */
   protected $parent;


   /**
    * Set the base constraints on the relation query.
    *
    * @return void
    */
   public function addConstraints()
   {
       // ...
   }


   /**
    * Set the constraints for an eager load of the relation.
    *
    * @param  array  $apps  An array of parent models
    * @return void
    */
   public function addEagerConstraints(array $apps)
   {
       // ...
   }


   /**
    * Match the eagerly loaded results to their parents.
    *
    * @param  array  $apps  An array of parent models
    * @param  \Illuminate\Database\Eloquent\Collection  $results  The result of the query executed by our relation class.
    * @param  string  $relation  The name of the relation
    * @return array
    */
   public function match(array $apps, Collection $results, $relation)
   {
       // ...
   }
}

Implementing the Custom Relation Class

The UserPermissionRelation class initializes two properties:

  • $this->query provides access to the related Permission model's query builder instance
  • $this->parent provides access to the parent User model

In order to define the users/permissions relationship, the following methods should be implemented:

addConstraints

Sets the base constraints on the relation query. In our example:

public function addConstraints()
{
    $this->query
        ->join('permission_role', 'permission_role.permission_id', '=', 'permissions.id')
        ->join('roles', 'permission_role.role_id', '=', 'roles.id')
        ->join('role_user', 'role_user.role_id', '=', 'roles.id');

    // If relation is not eager loaded
    if (!is_null($this->parent->getAttribute('id'))) {
        $this->query->where('role_user.user_id', '=', $this->parent->getAttribute('id'));
    }
}

addEagerConstraints

Sets the constraints for an eager load of the relation. In our example:

public function addEagerConstraints(array $users)
{
    $this->query
        ->whereIn('role_user.user_id', collect($users)->pluck('id'))
        ->with('roles.users'); // To avoid N+1 problem when eager loading
}

match

Matches the eagerly loaded results to their parents. In our example:

public function match(array $users, Collection $results, $relation)
{
    if ($results->isEmpty()) {
        return $users;
    }

    foreach ($users as $user) {
        $user->setRelation(
            $relation,
            $results->unique()->filter(function (Permission $permission) use ($user) {
                return in_array($user->id, $permission->roles->pluck('users.*.id')->flatten()->toArray());
            })->values()
        );
    }

    return $users;
}

Using the Custom Relation Class

Once the UserPermissionRelation class has been implemented, it can be used to define a new custom relationship between the User and the Permission model via relatesTo method which is available to the model through HasCustomRelation trait:

use CultureGr\CustomRelation\HasCustomRelation;

class User extends Model
{
    use HasCustomRelation;

    // ...

    public function permissions(): UserPermissionRelation
    {
        return $this->relatesTo(Permission::class, UserPermissionRelation::class);
    }
}

That's it 🔥! Now we can use our new custom permissions relationship like any usual Eloquent relationship:

// Use relationship as a method
$userPermissions = User::find('id')->permissions()->get();

// Use relationship as a dynamic property
$userPermissions = User::find('id')->permissions;

// Eager loading
$user = User::with('permissions')->where(/* ... */)->get();

// Lazy eager loading
$user = User::find('id');
$user->load('permissions');

Testing

composer test

License

Please see the license file for more information.

Credits

  • Awesome Laravel/PHP community

culturegr/custom-relation 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 11.58k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 15
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 4
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-11