sofa/eloquent-cascade
Composer 安装命令:
composer require sofa/eloquent-cascade
包简介
Cascading deletes for Laravel Eloquent ORM.
README 文档
README
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;
$model->delete()&$query->delete()w/o soft deletes- when using soft deletes ensure traits order:
use SoftDeletes, CascadeDeletes $model->delete()&$query->delete()with soft deletes$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 queryBuilder:>>> 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
SoftDeletesgets 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
restoringsoft 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sofa/eloquent-cascade 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Cascade attribute for Symfony Validator, reimplementing the Valid constraint in a more flexible and understandable way
A Yii2 cache component that cascades through multiple cache drivers on failure
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
统计信息
- 总下载量: 10.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 24
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-05-31