sofa/eloquent-cascade 问题修复 & 功能扩展

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

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

sofa/eloquent-cascade

Composer 安装命令:

composer require sofa/eloquent-cascade

包简介

Cascading deletes for Laravel Eloquent ORM.

README 文档

README

Downloads stable

Cascading (soft / hard) deletes for the Eloquent ORM (Laravel 5.0+).

Why use this one? There are couple of packages that already provide cascading deletes for eloquent, but none of them works with the query builder. Here you get support for both $model->delete() and $query->delete(), as well as $model->forceDelete().

Installation

Package goes along with Laravel (Illuminate) versioning, in order to make it easy for you to pick appropriate version:

Laravel / Illuminate 5.2+:

composer require sofa/eloquent-cascade:"~5.2"

Laravel / Illuminate 5.0/5.1:

composer require sofa/eloquent-cascade:"~5.1"

Usage

Use provided CascadeDeletes trait in your model and define relation to be deleted in cascade. Related models will be deleted automatically and appropriately, that is either hard or soft deleted, depending on the related model settings and delete method used:

tldr;

  1. $model->delete() & $query->delete() w/o soft deletes
  2. when using soft deletes ensure traits order: use SoftDeletes, CascadeDeletes
  3. $model->delete() & $query->delete() with soft deletes
  4. $model->forceDelete() BUT $query->forceDelete() will not work

simple:

<?php

namespace App;

use Sofa\EloquentCascade\CascadeDeletes;

class Product extends \Illuminate\Database\Eloquent\Model
{
    use CascadeDeletes;

    protected $deletesWith = ['types', 'photos'];
  • delete() called on the model:

    root@578687bd11c8:/var/www/html# php artisan tinker
    Psy Shell v0.7.2 (PHP 7.0.3 — cli) by Justin Hileman
    >>> DB::enableQueryLog()            
    => null
    >>> App\Product::find(200)->delete()
    => true
    >>> DB::getQueryLog()
    => [
         [
           "query" => "select * from `products` where `products`.`id` = ? limit 1",
           "bindings" => [200],
         ],
         [
           "query" => "delete from `products` where `id` = ?",
           "bindings" => [200],
         ],
         [
           "query" => "delete from `product_types` where `product_types`.`product_id` = ? and `product_types`.`product_id` is not null",
           "bindings" => [200],
         ],
         [
           "query" => "delete from `photos` where `photos`.`product_id` = ? and `photos`.`product_id` is not null",
           "bindings" => [200],
         ],
       ]
  • delete() called on the eloquent query Builder:

    >>> App\Product::whereIn('id', [202, 203])->delete()
    => 2
    >>> DB::getQueryLog()
    => [
         [
           "query" => "select * from `products` where `id` in (?, ?)",
           "bindings" => [202, 203],
         ],
         [
           "query" => "delete from `product_types` where `product_types`.`product_id` in (?, ?)",
           "bindings" => [202, 203],
         ],
         [
           "query" => "update `photos` set `deleted_at` = ?, `updated_at` = ? where `photos`.`product_id` in (?, ?) and `photos`.`deleted_at` is null",
           "bindings" => [
             "2016-05-31 09:44:41",
             "2016-05-31 09:44:41",
             202,
             203,
           ],
         ],
         [
           "query" => "delete from `products` where `id` in (?, ?)",
           "bindings" => [202, 203],
         ],
       ]

using with SoftDeletes

NOTE order of using traits matters, so make sure you use SoftDeletes before CascadeDeletes.

<?php

namespace App;

use Sofa\EloquentCascade\CascadeDeletes;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends \Illuminate\Database\Eloquent\Model
{
    use SoftDeletes, CascadeDeletes;

    // related Photo model uses SoftDeletes as well, but Type does not
    protected $deletesWith = ['types', 'photos'];
  • cascade with soft deletes - every model using SoftDeletes gets soft deleted, others are hard deleted

    >>> App\Product::whereIn('id', [300, 301])->delete()
    => 2
    >>> DB::getQueryLog()
    => [
         [
           "query" => "select * from `products` where `id` in (?, ?) and `products`.`deleted_at` is null",
           "bindings" => [300, 301],
         ],
         [
           "query" => "delete from `product_types` where `product_types`.`product_id` in (?, ?)",
           "bindings" => [300, 301],
         ],
         [
           "query" => "update `photos` set `deleted_at` = ?, `updated_at` = ? where `photos`.`product_id` in (?, ?) and `photos`.`deleted_at` is null",
           "bindings" => [
             "2016-05-31 09:52:30",
             "2016-05-31 09:52:30",
             300,
             301,
           ],
         ],
         [
           "query" => "update `products` set `deleted_at` = ?, `updated_at` = ? where `id` in (?, ?) and `products`.`deleted_at` is null",
           "bindings" => [
             "2016-05-31 09:52:30",
             "2016-05-31 09:52:30",
             300,
             301,
           ],
         ],
       ]
  • cascade with forceDelete() called on the model will hard-delete all the relations (NOTE due to the current implementation of forceDelete in laravel core, it will not work on the Builder)

    >>> App\Product::find(302)->forceDelete()
    => true
    >>> DB::getQueryLog()
    => [
         [
           "query" => "select * from `products` where `products`.`id` = ? and `products`.`deleted_at` is null limit 1",
           "bindings" => [302],
         ],
         [
           "query" => "delete from `products` where `id` = ?",
           "bindings" => [302],
         ],
         [
           "query" => "delete from `product_types` where `product_types`.`product_id` = ? and `product_types`.`product_id` is not null",
           "bindings" => [302],
         ],
         [
           "query" => "delete from `photos` where `photos`.`product_id` = ? and `photos`.`product_id` is not null",
           "bindings" => [302],
         ],
       ]

TODO

  • cascade restoring soft deleted models
  • detach m-m relations / delete related
  • add SET NULL and RESTRICT options (?)

Contribution

All contributions are welcome, PRs must be PSR-2 compliant.

CHANGELOG

v6 <- v5.x

  • Restoring now will cascade only for children that were deleted along with the parent model, not before. That is, if some of children models were soft deleted before the parent model got deleted, those children will not be restored when parent is being restored. That's the expected behavior.
  • The above requires that when calling restore on the query builder rather than single model ($query->restore() vs $model->restore()), it will run N queries, 1 for each restored model.

sofa/eloquent-cascade 适用场景与选型建议

sofa/eloquent-cascade 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.1k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2016 年 05 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 10.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 24
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 24
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-31