【问题标题】:What is the correct way to extend MySqlGrammar and bind to IOC in laravel在laravel中扩展MySqlGrammar并绑定到IOC的正确方法是什么
【发布时间】:2014-12-10 11:12:56
【问题描述】:

在我的工作台包中使用数据库时,我想扩展 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() 方法中执行此操作,在我看来,这似乎是放置此代码的最明显位置。

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

文件: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)."')";
    }
}

文件: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'));
    }
}

文件: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);
    }
}

文件: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 我在我的服务提供商中被覆盖的数据库类,任何帮助将不胜感激。

【问题讨论】:

    标签: mysql database laravel ioc-container


    【解决方案1】:

    请看一下我前段时间写的包 https://github.com/rafis/schema-extended

    【讨论】:

      【解决方案2】:

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

      文件: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);
          }
      }
      

      这应该可以解决问题!

      泽维尔

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-18
        • 2020-02-13
        • 2017-09-14
        • 1970-01-01
        • 2020-09-20
        相关资源
        最近更新 更多