3

我在 MySQL 数据库中使用 NDBCLUSTER 引擎。我添加了一个用于包装Connection和添加引擎选项的类:

namespace AppBundle\DBAL;

use Doctrine\DBAL\Connection as BaseConnection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Configuration;
use Doctrine\Common\EventManager;

class Connection extends BaseConnection
{
    public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null)
    {

        if (isset($params['driverOptions']['engine'])) {
            $params['defaultTableOptions']['engine'] = $params['driverOptions']['engine'];
        }

        return parent::__construct($params, $driver, $config, $eventManager);
    }
}

我在文件中定义了engine选项config.yml

doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   "%database_driver%"
                host:     "%database_host%"
                port:     "%database_port%"
                dbname:   "%database_name%"
                user:     "%database_user%"
                password: "%database_password%"
                charset:  UTF8
                wrapper_class: AppBundle\DBAL\Connection
                options:
                    engine: NDBCLUSTER
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        default_entity_manager: default
        entity_managers:
            default:
                connection: default
                mappings:
                    AppBundle:  ~

然后,如果我执行php app/console doctrine:migrations:diffNDBCLUSTER 引擎,则将其添加到CREATE语句中。但是,也添加了外键,并且 NDBCLUSTER 不接受外键。有什么方法可以禁用外键(我的意思是,不要将它们写在迁移文件中)?

4

1 回答 1

1

我通过为该连接 实现我自己的platform_service禁用了外键:

namespace AcmeBundle\Doctrine;
use Doctrine\DBAL\Platforms\MySQL57Platform;

class CustomMySQLPlatform extends MySQL57Platform
{

    public function supportsForeignKeyConstraints()
    {
        return false;
    }

    public function supportsForeignKeyOnUpdate()
    {
        return false;
    }
}

services.yml 中的服务定义:

    acme.dbal.service.custom_mysql_platform:
        class: AcmeBundle\Doctrine\CustomMySQLPlatform

config.yml 中的 Doctrine DBAL 定义:

doctrine:
    dbal:
        connections:
            default:
                ....
                platform_service: acme.dbal.service.custom_mysql_platform
于 2018-01-22T09:16:05.077 回答