在Laravel 5中扩展SQLite的蓝图 [英] Extending Blueprint for SQLite in Laravel 5

查看:68
本文介绍了在Laravel 5中扩展SQLite的蓝图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的Blueprint类,为了使用它,我遵循了此要点中的步骤

I have a custom Blueprint class, In order to use it I followed the steps in this gist.

基本上,它说我需要扩展MySqlConnection并使用Laravel在 Schema 文件夹中方便地使用的MySqlBuilder.

Basically, it says I need to extend a MySqlConnection and use a MySqlBuilder that Laravel conviniently has in the Schema folder.

对于MySQL来说效果很好!问题是我正在使用SQLite进行测试,我在那里也需要Bluepint,问题是没有基础SqliteConnectionSqliteBuilder ...

It worked great for MySQL! The problem is I'm using SQLite for testing, and I need the Bluepint there too, the issue is there is no base SqliteConnection nor SqliteBuilder...

当然,我得到以下异常:

Of course, I get the following Exception:

Argument 1 passed to CreateUsersTable::{closure}() 
must be an instance of MyBlueprint
instance of Illuminate\Database\Schema\Blueprint given

我必须自己编写那些缺少的课程吗?希望不会

Do I have to write those missing classes myself? Hope not

顺便说一句,对于Laravel 4,已经在这里提出了这个问题,但我不想在每次迁移时手动绑定我的蓝图...

By the way, this question has been asked here for Laravel 4 but I don't want to bind my Blueprint by hand on every migration...

推荐答案

好的,我关于SQLiteConnection丢失的陈述是错误的,它确实存在,并且显然不需要SQLiteBuilder MySqlBuilder必须是一个特殊的例外.

Alrighty, my statement about SQLiteConnection missing was wrong, it does exists and apparantly there's no need for a SQLiteBuilder so MySqlBuilder must be a special exception.

我对SQLiteConnection进行了如下扩展:

I extended the SQLiteConnection as follows:

use Illuminate\Database\SQLiteConnection as ParentSQLiteConnection;

class SQLiteConnection extends ParentSQLiteConnection
{
    public function getSchemaBuilder() {

        $builder = parent::getSchemaBuilder();

        $builder->blueprintResolver(function($table, $callback){

            return new MyBlueprint($table, $callback);
        });

        return $builder;
    }
}

然后在服务注册中:

class MyServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->app->bind('db.connection.sqlite', 'SQLiteConnection');
    }
}

为简洁起见,我省略了一些部分,主要是名称空间和放置服务的位置,但是所有这些都可以从我提出的问题的要旨中推论或查找.

I ommited some parts for brevity, mainly namespaces and where to place the service, but all of that can be deduced or looked up from the gist posted on my question as an example.

希望这可以帮助迷失的灵魂在将来找到自己的出路.

Hope this help lost souls find their way in the future.

这篇关于在Laravel 5中扩展SQLite的蓝图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆