承接 caneara/triggers 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 8.3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 35
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 35
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-04