在laravel中扩展MySqlGrammar并绑定到IOC的正确方法是什么

时间:2014-10-15 11:31:51

标签: mysql database laravel ioc-container

我想扩展MySqlGrammer,以便在我的工作台包中使用数据库时为MySql列类型'set'提供支持。

在寻找其他人尝试这样做的过程中,我发现了一些看似符合我想要的例子:

我尝试复制上面的前两种方法,但这两种方法在我尝试迁移时都会产生错误。第一种方法给出:

[Illuminate\Database\QueryException]
SQLSTATE[HY093]: Invalid parameter number (SQL: select * from information_schema.tables where table_schema = services_migrations and table_name = ?)

第二种方法给出:

[ErrorException]
Argument 1 passed to Illuminate\Database\Connection::__construct() must be an instance of PDO, array given, ...

第三种方法乍一看似乎工作正常,它在我的数据库表中创建了正确的列,但是当我在配置中使用表前缀时,此方法无法使用表前缀,我不得不手动为我的表名添加前缀让它发挥作用。

我很确定上面示例中的重写类是正确的方法,问题似乎是如何正确绑定和覆盖/扩展laravel以通知它类更改。目前我正在尝试在我的服务提供者register()方法中执行此操作,这在我看来是放置此代码的最明显的地方。

作为参考,我使用的代码如下所示:

file:workbench / iccle / quake3 / src / Iccle / Quake3 / Database / Schema / Grammars / MySqlGrammar.php

<?php 
namespace Iccle\Quake3\Database\Schema\Grammars;

class MySqlGrammar extends \Illuminate\Database\Schema\Grammars\MySqlGrammar
{
    protected function typeSet(\Illuminate\Support\Fluent $column)
    {
        return "set('".implode("', '", $column->allowed)."')";
    }
}

file:workbench / iccle / quake3 / src / Iccle / Quake3 / Database / Schema / Blueprint.php

<?php namespace Iccle\Quake3\Database\Schema;

class Blueprint extends \Illuminate\Database\Schema\Blueprint
{
    public function set($column, array $allowed)
    {
        return $this->addColumn('set', $column, compact('allowed'));
    }
}

file:workbench / iccle / quake3 / src / Iccle / Quake3 / Database / Schema / Builder.php

<?php 
namespace Iccle\Quake3\Database\Schema;

class Builder extends \Illuminate\Database\Schema\Builder
{
    protected function createBlueprint($table, Closure $callback = null)
    {
        return new \Iccle\Quake3\Database\Schema\Blueprint($table, $callback);
    }
}

file:workbench / iccle / quake3 / src / Iccle / Quake3 / Database / MySqlConnection.php

<?php namespace Iccle\Quake3\Database;

class MySqlConnection extends \Illuminate\Database\MySqlConnection
{
    protected function getDefaultSchemaGrammar()
    {
        return $this->withTablePrefix(new \Iccle\Quake3\Database\Schema\Grammars\MySqlGrammar);
    }

    public function getSchemaBuilder()
    {
        if (is_null($this->schemaGrammar)) { $this->useDefaultSchemaGrammar(); }

        return new \Iccle\Quake3\Database\Schema\Builder($this);
    }
}

文件:workbench / iccle / quake3 / src / Iccle / Quake3 / Quake3ServiceProvider.php

<?php namespace Iccle\Quake3;

use \Illuminate\Support\ServiceProvider;

class Quake3ServiceProvider extends ServiceProvider {

    protected $defer = false;

    public function boot()
    {
        $this->package('iccle/quake3');

        require_once(__DIR__ . '/../../routes.php');
        require_once(__DIR__ . '/../../filters.php');
    }

    public function register()
    {
        $app = $this->app;

        //Method 1, causes exceptions during migrations
        // $this->app->singleton('db.connection.mysql', function($app, $parameters)
        // {
        //  list($connection, $database, $prefix, $config) = $parameters;
        //  return new \Iccle\Quake3\Database\MySqlConnection($connection, $database, $prefix, $config);
        // });

        //Method 2, doomed to failure because it does not pass enough parameters to \Illuminate\Database\Connection::__construct
        //$this->app->resolving('db', function($db) {
        //  $db->extend('mysql', function($config) {
        //      return new \Iccle\Quake3\Database\MySqlConnection($config);
        //  });
        //});
    }


    public function provides()
    {
        return array();
    }

}

我的问题很简单,在我的服务提供商中通知laravel我所覆盖的数据库类的建议/正确方法是什么,非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

请看一下我刚才写的包裹 https://github.com/rafis/schema-extended

答案 1 :(得分:1)

我遇到了同样的错误,所以其他人可能会觉得这很有用。 Laravel有一个特殊的Builder for Mysql,它不是默认的构建器。因此,在您的代码中,您需要指定这个特殊的构建器:

file:workbench / iccle / quake3 / src / Iccle / Quake3 / Database / Schema / Builder.php

<?php 
namespace Iccle\Quake3\Database\Schema;

class Builder extends \Illuminate\Database\Schema\MySqlBuilder
{
    protected function createBlueprint($table, Closure $callback = null)
    {
        return new \Iccle\Quake3\Database\Schema\Blueprint($table, $callback);
    }
}

这应该可以做到!

相关问题