fico7489/laravel-updated-related 问题修复 & 功能扩展

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

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

fico7489/laravel-updated-related

Composer 安装命令:

composer require fico7489/laravel-updated-related

包简介

Update Elasticsearch data or clear cache when the model or any related model is updated, created or deleted.

README 文档

README

Update Elasticsearch data or clear cache when the model or any related model is updated, created or deleted.

Why to use

In laravel, we have the ability to listen to changes on eloquent models(create, delete, update), and when the model is changed we can do something with model e.g. we can flush cache for this model.

But sometimes we want to do something with the model when this model is changed or related models are changed. For example, you have model Seller with related models Order, ZipCodes, Address and OrderItem. If any change on Seller, Address, Order, ZipCode or OrderItem is performed you want to do something with Seller e.g flush cache for seller because cache include related models data:

$seller = Cache::remember('seller_' . $id, $minutes, function () use($id){
    return Seller::with(['orders', 'orders.items', 'addresses', 'zipCodes'])->find($id);
});

In above case when Seller model is changed, we have to flush his cache 'seller'.$id but also when his particular related models are updated we have to flush data. Obviously we need some configuration with related models here and this package will help you with this.

You can add this functionality without the package, but there is another huge problem. You can map related models and parent model to know when to flush cache but if you have batch form, e.g. for update all ZipCodes, and if you have 1000 zip codes and hit save, cache will be flushed 1000 times. This is not a problem for a flushing cache but if you have to update Elasticsearch data or perform some other time-consuming operation then it is.

This package also solves above problem because model changed events are saved to an array and processed after the request when they are filtered to be unique, so if you update 1000 zip codes for the same seller only one event will be dispatched.

Version Compatibility

The package is available for larvel 5.* versions.

Install

1.Install package with composer

composer require fico7489/laravel-updated-related:"*"

2.Add service provider to config/app.php

Fico7489\Laravel\UpdatedRelated\Providers\UpdatedRelatedServiceProvider::class

3.Publish configuration

php artisan vendor:publish  --provider="Fico7489\Laravel\UpdatedRelated\Providers\UpdatedRelatedServiceProvider"

and after that adjust configuration(map model with related models), see more in below section.

4.Use this trait

Fico7489\Laravel\UpdatedRelated\Traits\UpdatedRelatedTrait 

in your base model.

and that's it, you are ready to go.

Configuration

Configuration is located at config/laravel-updated-related.php

1.You can create configuration in simple way :

return [
    \App\Models\User::class => [
        [
            \App\Models\Address::class         => 'addresses',
            \App\Models\Order::class           => 'orders',
            \App\Models\OrderItem::class       => 'orders.items',
        ],
    ],
];

KEYS are base models, VALUES are arrays with related model => relation

2.Or you can create configuration in detailed way if you need more environments for the same base model. :

return [
    \App\Models\User::class => [
        [
            'name' => 'user-simple',
            'related' => [
                \App\Models\Address::class  => 'addresses',
            ],
        ],
        [
            'name' => 'user-extended',
            'related' => [
                \App\Models\Address::class   => 'addresses',
                \App\Models\OrderItem::class => 'orders.items',
            ],
        ],
    ]
];

KEYS are base models, VALUES are arrays with names (environment name) and related configuration (arrays with related model => relation). In "simple way" environment will be 'default'.

After you set a configuration just listened to the ModelChanged event that will be dispatched when any model or its related model (defined in configuration) is changed (updated, deleted, created).

One real example

Configuration :

return [
    \App\Models\User::class => [
        [
            \App\Models\Address::class => 'addresses',
        ],
    ],
];

Models :

...
class User extends BaseModel
{
    public function addresses()
    {
        return $this->hasMany(Address::class);
    }
...
...
class Address extends BaseModel
{
....

Event service provider :

...
protected $listen = [
    ModelChanged::class => [
        TestListener::class,
    ],
...

Listener :

use Fico7489\Laravel\UpdatedRelated\Events\ModelChanged;

class TestListener
{
    public function handle(ModelChanged $event)
    {
        echo 'id=' . $event->getId();
        echo 'model=' . $event->getModel();
        echo 'environment=' . $event->getName();
    }
}
User::find(1)->touch();
Address::find(2)->touch(); //let's assume that user_id is 10 here

With above code you will se this output:

id=1
model=App\Models\User
environment=default

id=10
model=App\Models\User
environment=default

If you create, delete or update User or Address model ModelChanged will be dispatched.

Cover all changes in the database

Do not use code like this one:

User::whereIn('id', $ids)->update(['status' => 'ban']);

Because in that case, laravel does not use a model, it runs query directly without a model. To cover above changes you can change this code to :

$users = User::whereIn('id', $ids);
foreach($users as $user){
    $user->update(['status' => 'ban']);
}

Pivot events

If you want to cover pivot events use this package : https://github.com/fico7489/laravel-pivot

When to use this package

  • clear cache for model
  • update elasticsearch data
  • and much more

License

MIT

Free Software, Hell Yeah!

fico7489/laravel-updated-related 适用场景与选型建议

fico7489/laravel-updated-related 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 494 次下载、GitHub Stars 达 19, 最近一次更新时间为 2017 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 fico7489/laravel-updated-related 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 494
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-02