我正在將我們的 Symfony 應用程式從 3.4 版升級到 4.4 版,其中包括將 Doctrine 從 1.12 升級到 2.3。我以前寫過一個類,將結果修改為doctrine:schema:update command,效果很好,但現在似乎不起作用。課程如下。
為了修改doctrine:schema:update,我創建了一個名為 的類DoctrineUpdateCommand,它擴展了\Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand,并將其放置在包的 Command 檔案夾中。這就是所需要的。我參考了這個答案來弄清楚如何去做:How to set up entity (doctrine) for database view in Symfony 2。
我們需要覆寫該doctrine:schema:update命令,因為我們的物體之一參考 MySQL 視圖而不是 MySQL 表。此外,物體被參考為獨立物體和多對多連接。覆寫類導致物體和連接被忽略。它還添加了一個 sql 陳述句來創建視圖。
升級后,如果我運行php console doctrine:schema:update --dump-sql,我會得到以下結果:
19:08:16 CRITICAL [console] Error thrown while running command "doctrine:schema:update --dump-sql". Message: "The table with name 'nest_qa.assignedrole_privilegerole' already exists." ["exception" => Doctrine\DBAL\Schema\SchemaException^ { …},"command" => "doctrine:schema:update --dump-sql","message" => "The table with name 'nest_qa.assignedrole_privilegerole' already exists."]
In SchemaException.php line 112:
The table with name 'nest_qa.assignedrole_privilegerole' already exists.
我相當確定不再呼叫該命令命令的擴展,而是使用默認命令,但我不知道如何更改它。任何幫助,將不勝感激。
這是原始類:
<?php
namespace ApiBundle\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Doctrine\ORM\Tools\SchemaTool;
class DoctrineUpdateCommand extends \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand{
protected $ignoredEntities = array(
'ApiBundle\Entity\AssignedrolePrivilegerole',
);
protected $ignoreAssociationMappings = array(
'ApiBundle\Entity\Privilegerole' => 'assignedroles',
'ApiBundle\Entity\Assignedrole' => 'privilegeroles',
);
protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas, SymfonyStyle $ui) {
/** @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */
$newMetadatas = array();
foreach ($metadatas as $metadata) {
if (array_key_exists($metadata->getName(), $this->ignoreAssociationMappings)) {
if(array_key_exists($this->ignoreAssociationMappings[$metadata->getName()], $metadata->getAssociationMappings())){
unset($metadata->associationMappings[$this->ignoreAssociationMappings[$metadata->getName()]]);
}
}
//If metadata element is not in the ignore array, add it to the new metadata array
if (!in_array($metadata->getName(), $this->ignoredEntities)){
array_push($newMetadatas, $metadata);
}
}
parent::executeSchemaCommand($input, $output, $schemaTool, $newMetadatas, $ui);
$output->writeln("------Create view for assignedrole_privilegerole");
$output->writeln("CREATE VIEW `assignedrole_privilegerole` AS select `Assignedrole`.`id` AS `assignedrole_id`,`Privilegerole`.`id` AS `privilegerole_id` from (`Assignedrole` join `Privilegerole`) where ((`Assignedrole`.`role_id` = `Privilegerole`.`role_id`) and ((`Assignedrole`.`unit_id` = `Privilegerole`.`unit_id`) or `Privilegerole`.`unit_id` in (select `Unittree`.`ancestor_id` from `Unittree` where (`Unittree`.`descendant_id` = `Assignedrole`.`unit_id`)))) ;");
}
}
uj5u.com熱心網友回復:
symfony <4.x 中的控制臺命令是通過掃描Command包內的檔案夾注冊的。由于捆綁在 symfony 4 中已過時,您必須通過標記命令類或使用 DI 自動配置在服務定義中定義命令。
選項 1:顯式添加console.command 標簽到服務定義:
services:
ApiBundle\Command\DoctrineUpdateCommand:
tags:
- { name: twig.extension }
選項 2:使用 DI自動配置:
services:
ApiBundle\Command\DoctrineUpdateCommand:
autoconfigure: true
在您的類注冊為控制臺命令后,它必須覆寫默認的。
有關更多資訊,請參閱 symfony 檔案:控制臺命令
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/385425.html
