symfony - 在 Doctrine 迁移中禁用外键

标签 symfony doctrine-orm foreign-keys doctrine-migrations

我在 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);
    }
}

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

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/consoledoctrine:migrations:diff NDBCLUSTER 引擎将添加到 CREATE 语句中。但是,外键也被添加,并且 NDBCLUSTER 不接受外键。有什么方法可以禁用外键(我的意思是,不将它们写入迁移文件中)?

最佳答案

我通过为该连接实现我自己的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

关于symfony - 在 Doctrine 迁移中禁用外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45054404/

相关文章:

php - 我应该为我的 Doctrine 模型使用抽象类还是接口(interface)?

symfony - 在symfony2中调用控制台函数

php - 使用 Bisna/Doctrine2 处理关闭的 EntityManager

Symfony2 - DoctrineMongoDBBundle - Doctrine\Common\Annotations\AnnotationException

SQL Server 多个表的外键

mysql - MySQL推断随机查询的外键关系

symfony - laravel 4.1.28 ,symfony/security 建议包

php - CakePHP 3 错误 : The application is trying to load a file from the DebugKit plugin

url - Symfony2 url层次结构

mysql - 仅当另一个外键匹配时才允许外键插入