Laravel开发-laravel-cascade-deletes

  • d9_234670
    了解作者
  • 19.7KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-15 05:25
    上传日期
Laravel开发-laravel-cascade-deletes 将应用程序级级联删除添加到雄辩的模型中。
Laravel开发-laravel-cascade-deletes .zip
  • laravel-cascade-deletes-master
  • composer.json
    1.3KB
  • .travis.yml
    170B
  • phpunit.xml
    746B
  • .scrutinizer.yml
    316B
  • phpcs.xml
    344B
  • tests
  • Models
  • SoftPost.php
    784B
  • Photo.php
    245B
  • User.php
    1.2KB
  • Post.php
    1.2KB
  • Profile.php
    299B
  • SoftProfile.php
    339B
  • SoftUser.php
    814B
  • ExtendedUser.php
    1.1KB
  • Comment.php
    429B
  • InvalidKid.php
    384B
  • PermanentPost.php
    790B
  • IntegrationTest.php
    14.5KB
  • helpers.php
    990B
  • TraitTest.php
    6.3KB
  • ModelTest.php
    387B
  • TestCase.php
    3KB
  • CONTRIBUTING.md
    1.3KB
  • src
  • CascadesDeletes.php
    7.4KB
  • CascadesDeletesModel.php
    165B
  • LICENSE.md
    1.1KB
  • .gitignore
    22B
  • README.md
    7.5KB
内容介绍
# laravel-cascade-deletes [![Latest Version on Packagist][ico-version]][link-packagist] [![Software License][ico-license]](LICENSE.txt) [![Build Status][ico-travis]][link-travis] [![Coverage Status][ico-scrutinizer]][link-scrutinizer] [![Quality Score][ico-code-quality]][link-code-quality] [![Total Downloads][ico-downloads]][link-downloads] This Laravel/Lumen package provides application level cascading deletes for the Laravel's Eloquent ORM. When referential integrity is not, or cannot be, enforced at the data storage level, this package makes it easy to set this up at the application level. For example, if you are using `SoftDeletes`, or are using polymorphic relationships, these are situations where foreign keys in the database cannot enforce referential integrity, and the application needs to step in. This package can help. ## Install Via Composer ``` bash $ composer require shiftonelabs/laravel-cascade-deletes ``` ## Usage Enabling cascading deletes can be done two ways. Either: - update your `Model` to extend the `\ShiftOneLabs\LaravelCascadeDeletes\CascadesDeletesModel`, or - update your `Model` to use the `\ShiftOneLabs\LaravelCascadeDeletes\CascadesDeletes` trait. Once that is done, define the `$cascadeDeletes` property on the `Model`. The `$cascadeDeletes` property should be set to an array of the relationships that should be deleted when a parent record is deleted. Now, when a parent record is deleted, the defined child records will also be deleted. Furthermore, in the case where a child record also has cascading deletes defined, the delete will cascade down and delete the related records of the child, as well. This will continue on until all children, grandchildren, great grandchildren, etc. are deleted. Additionally, all cascading deletes are performed within a transaction. This makes the delete an "all or nothing" event. If, for any reason, a child record could not be deleted, the transaction will rollback and no records will be deleted at all. The `Exception` that caused the child not to be deleted will bubble up to where the `delete()` originally started, and will need to be caught and handled.** #### Code Example User Model: ``` php <?php namespace App; use Illuminate\Database\Eloquent\Model; use ShiftOneLabs\LaravelCascadeDeletes\CascadesDeletes; class User extends Model { use CascadesDeletes; protected $cascadeDeletes = ['posts', 'profile']; public function posts() { return $this->hasMany(Post::class); } public function profile() { return $this->hasOne(Profile::class); } public function type() { return $this->belongsTo(Type::class); } } ``` Profile Model: ``` php <?php namespace App; use Illuminate\Database\Eloquent\Model; use ShiftOneLabs\LaravelCascadeDeletes\CascadesDeletes; class Profile extends Model { use CascadesDeletes; protected $cascadeDeletes = ['addresses']; public function user() { return $this->belongsTo(User::class); } public function addresses() { return $this->morphsMany(Address::class, 'addressable'); } } ``` In the example above, the `CascadesDeletes` trait has been added to the `User` model to enable cascading deletes. Since the user is considered a parent of posts and profiles, these relationships have been added to the `$cascadeDeletes` property. Additionally, the `Profile` model has been set up to delete its related address records. Given this setup, when a user record is deleted, all related posts and profile records will be deleted. The delete will also cascade down into the profile record, and it will delete all the addresses related to the profile, as well. If any one of the posts, profiles, or addresses fails to be deleted, the transaction will roll back and no records will be deleted, including the original user record.** ** Transaction rollback will only occur if the database being used actually supports transactions. Most do, but some do not. For example, the MySQL `InnoDB` engine supports transactions, but the MySQL `MyISAM` engine does not. #### SoftDeletes This package also works with Models that are setup with `SoftDeletes`. When using `SoftDeletes`, the delete method being used will cascade to the rest of the deletes, as well. That is, if you `delete()` a record, all the child records will also use `delete()`; if you `forceDelete()` a record, all the child records will also use `forceDelete()`. The deletes will also cross the boundary between soft deletes and hard deletes. In the code example above, the the `User` record was setup to soft delete, but the `Profile` record was not, then when a user is deleted, the `User` record would be soft deleted, but the child `Profile` record would be hard deleted, and vice versa. ## Notes - The functionality in this package is provided through the `deleting` event on the `Model`. Therefore, in order for the cascading deletes to work, `delete()` must be called on a model instance. Deletes will not cascade if a delete is performed through the query builder. For example, `App\User::where('active', 0)->delete();` will only delete those user records, and will not perform any cascading deletes, since the `delete()` was performed on the query builder and not on a model instance. - Do not add a `BelongsTo` relationship to the `$cascadeDeletes` array. This will cause a `LogicException`, and no records will be deleted. This is done as a `BelongsTo` typically represents a child record, and it usually does not make sense to delete a parent record from a child record. - This package works with the `illuminate/database` package from `4.1` through `5.3`, meaning Laravel 4.1+, Laravel 5.0+, and Lumen 5.0+ are all supported. However, if using `4.1` specifically, there is one additional piece of setup. Since `4.1` does not automatically boot traits, you will need to add the following method to your model: ``` php protected static function boot { parent::boot(); static::bootCascadesDeletes(); } ``` ## Contributing Contributions are welcome. Please see [CONTRIBUTING](CONTRIBUTING.md) for details. ## Security If you discover any security related issues, please email patrick@shiftonelabs.com instead of using the issue tracker. ## Credits - [Patrick Carlo-Hickman][link-author] - [All Contributors][link-contributors] ## License The MIT License (MIT). Please see [License File](LICENSE.txt) for more information. [ico-version]: https://img.shields.io/packagist/v/shiftonelabs/laravel-cascade-deletes.svg?style=flat-square [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square [ico-travis]: https://img.shields.io/travis/shiftonelabs/laravel-cascade-deletes/master.svg?style=flat-square [ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/shiftonelabs/laravel-cascade-deletes.svg?style=flat-square [ico-code-quality]: https://img.shields.io/scrutinizer/g/shiftonelabs/laravel-cascade-deletes.svg?style=flat-square [ico-downloads]: https://img.shields.io/packagist/dt/shiftonelabs/laravel-cascade-deletes.svg?style=flat-square [link-packagist]: https://packagist.org/packages/shiftonelabs/laravel-cascade-deletes [link-travis]: https://travis-ci.org/shiftonelabs/laravel-cascade-deletes [link-scrutinizer]: https://scrutinizer-ci.com/g/shiftonelabs/laravel-cascade-deletes/code-structure [link-code-quality]: https://scrutinizer-ci.com/g/shiftonelabs/laravel-cascade-deletes [link-downloads]: https://packagist.org/packages/shiftonelabs/laravel-cascade-deletes [link-author]: https://github.com/patrickcarlohickman [link-contributors]: ../../contributors
评论
    相关推荐
    • laravel
      关于Laravel Laravel是一个具有表达力,优雅语法的Web应用程序框架。 我们认为,发展必须是一种令人愉快的,富有创造力的经历,才能真正实现。 Laravel减轻了许多Web项目中使用的常见任务,从而减轻了开发过程中的...
    • Laravel
      关于Laravel Laravel是一个具有表达力,优雅语法的Web应用程序框架。 我们认为,发展必须是一种令人愉悦的创造力,才能真正实现。 Laravel通过减轻许多Web项目中使用的常见任务来减轻开发工作的痛苦,例如: 。 。...
    • Laravel
      Laravel
    • laravel
      关于Laravel Laravel是一个Web应用程序框架,具有表达力强,优雅的语法。 我们认为,发展必须是一种令人愉快的,富有创造力的经历,才能真正实现。 Laravel减轻了许多Web项目中使用的常见任务,从而减轻了开发过程中...
    • Laravel
      Laravel
    • laravel
      关于Laravel Laravel是一个具有表达力,优雅语法的Web应用程序框架。 我们认为,发展必须是一种令人愉快的,富有创造力的经历,才能真正实现。 Laravel减轻了许多Web项目中使用的常见任务,从而减轻了开发过程中的...
    • laravel
      关于Laravel Laravel是一个具有表达力,优雅语法的Web应用程序框架。 我们认为,发展必须是一种令人愉快的,富有创造力的经历,才能真正实现。 Laravel减轻了许多Web项目中使用的常见任务,从而减轻了开发过程中的...
    • Laravel
      Laravel
    • laravel
      laravel
    • Laravel
      Laravel