我如何停止对Symfony尝试创建我在学说迁移上创建的视图的表?
实体映射视图
/**
* Class TenancyPendingInspection
* @ORMEntity(repositoryClass="DJABundlePropertyVisitRepositoryTenancyPendingInspectionRepository", readOnly=true)
* @ORMTable(name="view_tenancies_pending_inspections")
*/
class TenancyPendingInspection
{
我也有学说迁移文件。
学说配置
doctrine:
dbal:
default_connection: default
connections:
default:
[...]
charset: UTF8
server_version: 5.6
schema_filter: ~^(?!view_)~
Doctirne Schema veration
php app/console doc:sch:val
[Mapping] OK - The mapping files are correct.
[Database] FAIL - The database schema is not in sync with the current mapping file.
学说架构更新
php app/console doc:sch:update --dump-sql
CREATE TABLE view_tenancies_pending_inspections ...
简短答案:不能完成。
这是因为MysqlPlatform
忽略了视图。
//vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
class MySqlPlatform extends AbstractPlatform
[...]
public function getListTablesSQL()
{
return "SHOW FULL TABLES WHERE Table_type = 'BASE TABLE'";
}
[...]
解决方案:创建一个不忽略视图的新的mysqlplatform:
class MysqlViewsPlatform extends DoctrineDBALPlatformsMySqlPlatform
{
public function getListTablesSQL()
{
return "SHOW FULL TABLES";
}
}
使用您的平台创建服务:
services:
doctrine.dbal.mysql_views_platform:
class: albertsolaDoctrineViewsMysqlViewsPlatform
arguments: []
使用该平台与您的连接:
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
platform_service: "doctrine.dbal.mysql_views_platform"
应用程序/控制台学说:架构:验证此命令验证了实体和视图实体是同步的。
副作用:App/Console学说:架构:更新-Dump -SQL如果视图和实体不同步,则会生成不应执行的SQL!您必须在数据库上手动更新视图。
我使用解决此问题的学说迁移。虽然学说:架构:更新-dump-sql对于检查视图/实体中不匹配的内容非常有用。
注意:此hack停止创建表,但学说架构差异仍然尝试更新添加外键的"表"。
这对我来说很好;
class DiffCommandWrapper extends DiffCommand
{
private $views = [];
public function __construct(PDO $db, SchemaProviderInterface $schemaProvider = null)
{
parent::__construct($schemaProvider);
$name = $db->query('select database()')->fetchColumn();
$stm = $db->query("SHOW FULL TABLES IN {$name} WHERE TABLE_TYPE LIKE 'VIEW';");
foreach ($stm->fetchAll(PDO::FETCH_ASSOC) as $row) {
$this->views[] = $row["Tables_in_{$name}"];
}
}
public function execute(InputInterface $input, OutputInterface $output)
{
$input->setOption('filter-expression', '/!(' . implode('|', $this->views) . ')/');
parent::execute($input, $output);
}
}
然后使用包装器代替cli-config.php中的diffcommand
ConsoleRunner::addCommands($cli);
$cli->addCommands([
new somethingDiffCommandWrapper($connection),
new CommandExecuteCommand(),
new CommandGenerateCommand(),
new CommandMigrateCommand(),
new CommandStatusCommand(),
new CommandVersionCommand(),
]);
您应该能够使用setFiltersChemaAssetSexpression。
http://www.doctrine-project.org/api/dbal/2.4/source-class-doctrine.dbal.configuration.html#87-99
失败,如果您想进行更少的编程和更多的配置,则可以将视图放入另一个实体管理器中。不是最好的方法....
https://symfony.com/doc/3.3/doctrine/multiple_entity_managers.html