Command/CreateSchemaDoctrineCommand.php
changeset 42 0e57c730bb18
parent 25 11fd79666374
child 74 901463f9b11c
equal deleted inserted replaced
39:b403086580f7 42:0e57c730bb18
    19 use Symfony\Component\Console\Output\OutputInterface;
    19 use Symfony\Component\Console\Output\OutputInterface;
    20 use Symfony\Component\Console\Output\Output;
    20 use Symfony\Component\Console\Output\Output;
    21 use Doctrine\ORM\Tools\SchemaTool;
    21 use Doctrine\ORM\Tools\SchemaTool;
    22 use Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand;
    22 use Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand;
    23 use Symfony\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper;
    23 use Symfony\Bundle\DoctrineBundle\Command\Proxy\DoctrineCommandHelper;
       
    24 use IRI\Bundle\WikiTagBundle\Utils\FilteredSchemaTool;
    24 
    25 
    25 /**
    26 /**
    26  * Command to execute the SQL needed to generate the database schema for
    27  * Command to execute the SQL needed to generate the database schema for
    27  * a given entity manager.
    28  * a given entity manager.
    28  *
    29  *
    76     public function setContainer(ContainerInterface $container = null)
    77     public function setContainer(ContainerInterface $container = null)
    77     {
    78     {
    78         $this->container = $container;
    79         $this->container = $container;
    79     }
    80     }
    80     
    81     
    81     
    82         
    82     protected function filterCreateSchema($sqls)
    83     protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas)
    83     {
    84     {
    84         
    85         
    85         // get service
    86         $filteredSchemaTool = new FilteredSchemaTool($this->getHelper("em")->getEntityManager(), $this->getContainer());
    86         $schema_utils = $this->getContainer()->get("wikitag.shema_utils");
       
    87         
    87         
    88         $res_sqls = $schema_utils->filter_foreign_key($sqls);
    88         parent::executeSchemaCommand($input, $output, $filteredSchemaTool, $metadatas);
    89         $res_sqls = $schema_utils->filter_index_creation($res_sqls);
    89                 
    90         
       
    91         
       
    92         return $res_sqls;
       
    93     }
       
    94     
       
    95     protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas)
       
    96     {
       
    97         $output->write('ATTENTION: This operation should not be executed in a production environment.' . PHP_EOL . PHP_EOL);
       
    98         $sqls = $schemaTool->getCreateSchemaSql($metadatas);
       
    99         
       
   100         $createSchemaSql = $this->filterCreateSchema($sqls);
       
   101         
       
   102         if ($input->getOption('dump-sql') === true) {
       
   103                         
       
   104             $output->write(implode(';' . PHP_EOL, $createSchemaSql) . PHP_EOL);
       
   105         } else {
       
   106             $output->write('Creating database schema...' . PHP_EOL);
       
   107             $emHelper = $this->getHelper('em');
       
   108             
       
   109             $conn = $emHelper->getEntityManager()->getConnection();
       
   110             
       
   111             foreach ($createSchemaSql as $sql) {
       
   112                 $conn->executeQuery($sql);
       
   113             }
       
   114             $output->write('Database schema created successfully!' . PHP_EOL);
       
   115         }
       
   116         
       
   117     }
    90     }
   118 
    91 
   119     protected function execute(InputInterface $input, OutputInterface $output)
    92     protected function execute(InputInterface $input, OutputInterface $output)
   120     {
    93     {
   121         DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));
    94         DoctrineCommandHelper::setApplicationEntityManager($this->getApplication(), $input->getOption('em'));