我在 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/