ajcastro/fk-adder 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

ajcastro/fk-adder

Composer 安装命令:

composer require ajcastro/fk-adder

包简介

Lets you add foreign keys in a swift! Foreign key adder in laravel migration.

README 文档

README

Lets you add foreign keys smart and easy! Foreign key adder for laravel migration. For Laravel 4.2 and 5.*.

The purpose of FkAdder is to simplify declaration of foreign key columns in migration.

Things that FkAdder do for you:
  • Remembers the data type of a foreign key, and it will provide the correct data type for you, so that you don't have to recall the data type of foreign key column whenever you need that particular foreign key.
  • Lets you create migration tables in any order. This solves the problem when your table is created prior than the reference table. Usually this may cause error like Reference table some_table does not exist, this happens when referencing the table which are to be migrated on last part of migrations.
  • Speeds up laravel migration development.

Installation

composer require ajcastro/fk-adder

Alias

Add alias into config/app.php file. You can skip this because of laravel's auto-discovery.

 'Fk' => FkAdder\Fk::class

Configuration

Create a config file named config/fk_adder.php

<?php

return [
    // For simple string-based declaration
    'fk_datatypes_path' => base_path('database/foreign_keys/fk_datatypes.php')
    // For class-based declaration, used for special cases and more control. You don't need this for simple cases .
    'fk_namespace' => 'Your\Fk\Namespace',
];

Setup

There are two ways to setup your foreign keys: string-based declaration and class-based declaration. String-based is preferred for simpler datatype declaration.

String-based declaration

Open your fk_datatypes_path file and add the foreign key declaration in the array. The array keys are the foreign key columns and its values are the datatypes. The reference tables are smartly guessed already by remove the _id and pluralizing the foreign key names e.g. user_id -> users.

<?php

return [
    'user_id'       => 'unsignedInteger',
    'group_id'      => 'unsignedInteger',
    'preference_id' => 'unsignedBigInteger',
];

Since version 1.2, you can now also define the reference table. This is helpful for foreign keys which has custom table names.

<?php

return [
    'user_id'       => 'unsignedInteger, custom_users',
    'group_id'      => 'unsignedInteger, custom_groups',
];
Class-based declaration

Create classes of foreign keys declaration inside your fk_namespace directory path.

Naming Convention

If the foreign key is e.g. user_id, then the class name should be UserId.

Example:

<?php

namespace Your\Fk\Namespace;

use FkAdder\BaseFk;

class UserId extends BaseFk
{
    protected $referenceTable = 'users';

    public function createFkColumn($column)
    {
        return $this->table->unsignedInteger($column);
    }
}

Sample Usage, Before vs After

Before

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('group_id')->nullable()->comment('Group of the user');
            $table->unsignedBigInteger('preference_id')->nullable()->comment('Preference of the user');

            $table->foreign('group_id')->references('id')->on('groups');
            $table->foreign('preference_id')->references('id')->on('preferences')
                ->onDelete('cascade')>onUpdate('cascade');
        });
    }
}

After

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table) {
            $table->increments('id');

            Fk::make($table)->add('group_id')->nullable()->comment('Group of the user');

            Fk::make($table)
                ->onDelete('cascade')
                ->onUpdate('cascade')
                ->add('preference_id')
                ->nullable()
                ->comment('Preference of the user');
        });

        Fk::migrate();
    }
}

More Features, Benefits and Explanations

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table) {
            $table->increments('id');

            // Foreign key declaration is one-liner, simpler and more compact.
            // You dont have to type what datatype it is. You will just declare it once.
            Fk::make($table)->add('group_id')->nullable()->comment('Group of the user');
            Fk::make($table)->onDelete('cascade')->add('preference_id')
                ->nullable()->comment('Preference of the user');

            // After you call the method `add()`, it will return an instance of the usual \Illuminate\Support\Fluent,
            // so that you can chain more column declaration like `nullable()` and `comment()`

            // If ever you need a different column name from the foreign key, just pass a second parameter
            // to `add()` method e.g.
            Fk::make($table)->add('group_id', 'new_group_id')->nullable()->comment('New group of the user');

            // The default `onDelete` settings is `restrict` and `onUpdate` is `cascade`.
            Fk::make($table)->onDelete('restrict')->onUpdate('cascade')->add('group_id', 'new_group_id');

            // You can also pass the key name for the foreign key.
            Fk::make($table)->keyName('users_new_group_id_foreign_key')->add('group_id', 'new_group_id');

            // Take note that those foreign key modifiers should be called prior or before the `add()` method.
        });

        // Finally, you may now migrate and persist foreign keys in mysql database.
        // You can call this once at the very end of migration,
        // so all your foreign key declarations accross different migration files will be persisted.
        Fk::migrate();
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Fk::rollback(); // delete foreign keys persisted by Fk::migrate(), (coming soon...)
        Schema::dropIfExists('users');
    }
}

License

Released under MIT License.

ajcastro/fk-adder 适用场景与选型建议

ajcastro/fk-adder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.62k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2016 年 06 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「migration」 「fk」 「foreign key」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ajcastro/fk-adder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ajcastro/fk-adder 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.62k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-11