jdavidbakr/replaceable-model
Composer 安装命令:
composer require jdavidbakr/replaceable-model
包简介
Adds 'REPLACE' and 'INSERT IGNORE' query capability to eloquent models
README 文档
README
The default Eloquent model is great for most cases, but if you have a database table that has additional constraints you may run into race conditions where the standard update() call will fail. Imagine, for example, the following table structure:
id auto increment
user_id
widget_id
date
where you have a constraint with each user can only have one widget per day, so you have a unique constraint across user_id and date. Now in your interface you have a form that uses ajax calls to update the entries in this table, which may include removing items based on which items are selected. Because the form submits, say, an entire month's worth of widgets, you don't want to loop through and do individual inserts - you want to perform a single insert query. So you have something like the following in your php code:
Model::where('user_id',$user_id)->delete(); Model::insert($inserts);
This essentially performs the following under the hood:
delete from table where user_id = A; insert into table (user_id, widget_id, date) values (A, B, C) ...
If you get stuck in a race condition, you might end up with the following:
delete from table where user_id = A; -- process #1 delete from table where user_id = A; -- process #2 insert into table (user_id, widget_id, date) values (A, B, C) ... -- process #1 insert into table (user_id, widget_id, date) values (A, B, C) ... -- process #2 - Exception!
The second insert will result in an exception. If the second query had an additional row than the first one, that insert is lost forever.
Before Laravel, I would normally have handled this type of situation with REPLACE or INSERT IGNORE commands. REPLACE will do a delete and insert based on any constraints in the query, and INSERT IGNORE will perform the insert but if there are any rows that cause constraint collisions those rows will not be updated. You would use REPLACE if you want the last query to overwrite any existing rows, and you would use INSERT IGNORE to only insert new rows.
Because this is a specific feature of MySQL, Laravel does not support it in Eloquent. The standard solution is to perform a raw query. This is ok, but it is kind of cumbersome to build this query every time with the bindings, etc, and I decided it would be helpful to create a trait for Eloquent that handles all of this for me and can be accessed in the same way that I would use insert().
Note that I'm NOT using the Builder class here. I'm directly extending the Model class and as such you won't be able to chain these functions like you might the regular insert() command. This is really just a macro to fix a problem that I had. I welcome any pull requests that solve additional problems you may have with this package.
The 'saving' and 'saved' events are fired for both of these commands.
Install
Via Composer
$ composer require jdavidbakr/replaceable-model
Usage
Apply the trait to your models to activate the ability to use replace and insertIgnore
class model extends Model { ... use \jdavidbakr\ReplaceableModel\ReplaceableModel ... }
Then build your insert array like you would for the insert() call and call one of the two functions:
$inserts = [...]; \App\Model::replace($inserts); \App\Model::insertIgnore($inserts);
Contributing
Please see CONTRIBUTING and CONDUCT for details.
Security
If you discover any security related issues, please email me@jdavidbaker.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
jdavidbakr/replaceable-model 适用场景与选型建议
jdavidbakr/replaceable-model 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 247.47k 次下载、GitHub Stars 达 72, 最近一次更新时间为 2016 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「jdavidbakr」 「replaceable-model」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jdavidbakr/replaceable-model 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jdavidbakr/replaceable-model 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jdavidbakr/replaceable-model 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Logs and tracks all outgoing emails from Laravel
A script that will clean up expired cache files if the system is using the files cache system
Sends a notification via SNS when there is an exception
Logs and tracks all outgoing emails from Laravel
A script that will clean up expired cache files if the system is using the files cache system
Service Provider to add the Swift ThrottlerPlugin to outgoing mail.
统计信息
- 总下载量: 247.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 72
- 点击次数: 19
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-07