定制 moataz27/schema-object-manager 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

moataz27/schema-object-manager

最新稳定版本:v1.0.2

Composer 安装命令:

composer require moataz27/schema-object-manager

包简介

Manage database views, procedures, functions and triggers without migrations

README 文档

README

A Laravel package to manage database schema objects (Views, Procedures, Functions, Triggers) as code. This package allows you to version control your database logic and sync it easily across different environments using Artisan commands, similar to how Laravel Migrations manage your tables.

Features

  • Manage Database Objects: First-class support for Views, Stored Procedures, Functions, and Triggers.
  • Version Control: Keep your database logic in your codebase, not hidden in the database.
  • Auto-Discovery: Automatically finds and registers your schema object classes.
  • Safe Syncing: Prevents accidental overwrites in production environments (requires --force).
  • Artisan Integration: Simple commands to create and sync your objects.

Installation

You can install the package via composer:

composer require moataz/schema-object-manager

Publish Configuration (Optional)

You can publish the configuration file to customize the namespace and other settings:

php artisan vendor:publish --tag="schema-objects"

This will create a config/schema-objects.php file where you can configure the namespace for your schema objects and toggle auto-discovery.

Usage

Creating Schema Objects

The package provides Artisan commands to generate boilerplate classes for your schema objects. By default, these are created in App\SchemaObjects.

Create a View:

php artisan schema:view UserStatsView

Create a Stored Procedure:

php artisan schema:procedure CalculatMonthlyRevenue

Create a Function:

php artisan schema:function GetUserAge

Create a Trigger:

php artisan schema:trigger BeforeUserUpdate

Defining Your Object

Once created, you can define the logic in the generated class. Here is an example of a View:

<?php

namespace App\SchemaObjects;

use Moataz\SchemaObjectManager\Objects\View;

class UserStatsView extends View
{
    /**
     * Get the name of the schema object.
     */
    public function name(): string
    {
        return 'user_stats_view';
    }

    /**
     * Get the SQL definition of the schema object.
     */
    public function definition(): string
    {
        return <<<'SQL'
        CREATE VIEW user_stats_view AS
        SELECT
            users.id,
            users.name,
            COUNT(orders.id) as total_orders
        FROM users
        LEFT JOIN orders ON orders.user_id = users.id
        GROUP BY users.id
        SQL;
    }
}

Syncing to Database

To apply your changes to the database, run the sync command:

php artisan schema:sync

This command will iterate through all discoverable schema objects and execute their definitions against the database.

Production Safety: If you run this command in a production environment, it will fail by default to prevent accidental changes. To force the sync in production, use the --force flag:

php artisan schema:sync --force

Configuration

The config/schema-objects.php file allows you to configure:

  • objects: Manually register object classes if auto-discovery is disabled.
  • namespace: The default namespace where new objects are created (default: App\SchemaObjects).
  • cache: Configuration for caching discovered objects to improve performance.
  • auto_discover: Enable or disable automatic discovery of schema objects in the configured namespace.

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固