balfour/eloquent-dynamic-relationships 问题修复 & 功能扩展

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

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

balfour/eloquent-dynamic-relationships

Composer 安装命令:

composer require balfour/eloquent-dynamic-relationships

包简介

A library for monkey patching relationships between Eloquent models at runtime

README 文档

README

A library for monkey patching relationships between Eloquent models at runtime.

You can use this library inside any Laravel application or stand-alone application which uses the Eloquent ORM.

This library is in early release and is pending unit tests.

Use Case

We developed this library when a need arose to bond models separated across multiple packages (and repositories) in an internal modular system.

Let's consider a system such as the following:

  • The framework - the brain which glues modules together.
  • A user module - a package which allows users to login, register, etc.
  • A sms module - a package which allows users to send text messages.

These are 3 separate packages stored in independent repositories which have the bare minimum knowledge of each other.

  • The framework knows about both user and sms modules.
  • The user module has no dependencies.
  • The sms module knows about the user module.

In this system, the Message model can define it's many to one relationship to the User model; however the User model has no context of the sms package and is therefore not capable of defining the inverse one to many relationship back.

eg: $message->user would succeed, but $user->messages or $user->messages()->paginate() would cause an error.

The responbility therefore falls on the framework to monkey patch this inverse relationship on the User model.

Installation

composer require balfour/eloquent-dynamic-relationships

This package only needs to be installed in the repository (or repositories) containing models you want to make monkey patachable.

Usage

Configuring Monkey Patachable Model

The model which you want to make monkey patchable needs to either extend our base model, or use the HasDynamicRelationships trait.

Method 1 (preferred)

namespace SMSModule;

use Balfour\EloquentDynamicRelationships\HasDynamicRelationships;
use Illuminate\Database\Eloquent\Model;

class Message extends Model
{
    use HasDynamicRelationships;
    
    //
}

Method 2

namespace SMSModule;

use Balfour\EloquentDynamicRelationships\Model;

class Message extends Model
{
    //   
}

Creating Dynamic Relationship

In your framework codebase which glues everything together, you'll need to bond the two models together. This should typically be done at a bootstrap stage, such as when your app's AppServiceProvider is booted.

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use SMSModule\Message;
use UserModule\User;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        UserModule::bond('messages', function () {
            return $this->hasMany(Message::class);
        });
    }
}

With the bond established between the 2 models, you can now use relationship calls as per normal.

eg:

use SMSModule\Message;
use UserModule\User;

$user = User::find(1);

// retrieve a user's messages
$messages = $user->messages;

// retrieve and sort a user's messages by send date
$messages = $user->messages()
    ->latest('send_date')
    ->get();
    
// paginate a user's messages
$messages = $user->messages()
    ->paginate();

// get the user who sent the message
$message = Message::first();
$user = $message->user;

Other Methods

use UserModule\User;

// destroy a bond between 2 models
User::breakup('activities');

// retrieve all dynamic bonds
$bonds = User::getBonds();

// determine if a model is bonded with another
User::isBondedWith('activities');

balfour/eloquent-dynamic-relationships 适用场景与选型建议

balfour/eloquent-dynamic-relationships 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 04 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 balfour/eloquent-dynamic-relationships 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-28