caneara/triggers
最新稳定版本:v1.0.1
Composer 安装命令:
composer require caneara/triggers
包简介
A package to add database trigger support to Laravel
README 文档
README
This package enables the use of database triggers within Laravel applications. Note that your chosen database must support triggers for the package to work.
Installation
Pull in the package using Composer:
composer require caneara/triggers
Usage
Triggers can only be added to existing tables. Therefore, when creating triggers in your migration files, make sure you add them after the Schema::create method.
Table
To create a trigger, simply call the table method on the Triggers\Trigger class:
use Triggers\Trigger; Trigger::table('posts');
Key
By default, the class will generate a name for the trigger using the following convention:
trigger_{TABLE}_{TIME}_{EVENT}
However, since trigger names must be unique across the database, if you were to create two triggers that used the same event and time (these concepts are covered in the next section), then you'd get an error.
To address this problem, the class offers a key method that allows you to add your own custom text to the trigger's name, thereby ensuring that the trigger name can be made unique:
Trigger::table('posts')->key('custom');
When a key is specified, the trigger name is derived from the following convention:
trigger_{TABLE}_{KEY}_{TIME}_{EVENT}
Event and time
Next, you need to specify whether the trigger should be fired for an INSERT, UPDATE or DELETE event. You will also need to specify whether the trigger should run BEFORE or AFTER the event has taken place:
Trigger::table('posts')->beforeDelete(); Trigger::table('posts')->beforeInsert(); Trigger::table('posts')->beforeUpdate(); Trigger::table('posts')->afterDelete(); Trigger::table('posts')->afterInsert(); Trigger::table('posts')->afterUpdate();
Statement
The final step, is to specify the SQL statement(s) that should be executed by the trigger when it is fired. To do this, supply a Closure to the event / time method. Note that the Closure must return a SQL string e.g.
Trigger::table('posts')->afterInsert(function() { return "UPDATE `users` SET `posts` = 1 WHERE `id` = NEW.user_id;"; });
Example
The following example shows a migration that creates a posts table and then assigns the trigger to it.
use Triggers\Trigger; class CreatePostsTable extends Migration { public function up() : void { Schema::create('posts', function(Blueprint $table) { $table->unsignedTinyInteger('id'); $table->string('title'); }); Trigger::table('posts')->key('count')->afterInsert(function() { return "UPDATE `users` SET `posts` = 1 WHERE `id` = NEW.user_id;"; }); } }
Contributing
Thank you for considering a contribution to the project. You are welcome to submit a PR containing improvements, however if they are substantial in nature, please also be sure to include a test or tests.
License
The MIT License (MIT). Please see License File for more information.
caneara/triggers 适用场景与选型建议
caneara/triggers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.3k 次下载、GitHub Stars 达 35, 最近一次更新时间为 2022 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「php」 「laravel」 「triggers」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 caneara/triggers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 caneara/triggers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 caneara/triggers 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
PHP module for MySql database
Alfabank REST API integration
统计信息
- 总下载量: 8.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 35
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-04