【问题标题】:Disable foreign keys in Doctrine Migrations在 Doctrine Migrations 中禁用外键
【发布时间】:2017-07-12 09:56:31
【问题描述】:

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

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2017-01-14
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      • 1970-01-01
      • 2019-01-30
      • 2022-11-23
      相关资源
      最近更新 更多