*/ class SqlInsertCommand extends AbstractCommand { /** * @see Command */ protected function configure() { $this ->setDescription('Insert SQL for current model') ->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action.') ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'Set this parameter to define a connection to use') ->setHelp(<<%command.name% command connects to the database and executes all SQL statements found in app/propel/sql/*schema.sql. php %command.full_name% The --force parameter has to be used to actually insert SQL. The --connection parameter allows you to change the connection to use. The default connection is the active connection (propel.dbal.default_connection). EOT ) ->setName('propel:sql:insert') ; } /** * @see Command * * @throws \InvalidArgumentException When the target directory does not exist */ protected function execute(InputInterface $input, OutputInterface $output) { // Bad require but needed :( require_once $this->getContainer()->getParameter('propel.path') . '/generator/lib/util/PropelSqlManager.php'; if ($input->getOption('force')) { if ($input->getOption('verbose')) { $this->additionalPhingArgs[] = 'verbose'; } $connections = $this->getConnections(); $sqlDir = $this->getSqlDir(); $manager = new \PropelSqlManager(); $manager->setWorkingDirectory($sqlDir); $manager->setConnections($connections); if ($input->getOption('connection')) { list($name, $config) = $this->getConnection($input, $output); $this->doSqlInsert($manager, $output, $name); } else { foreach ($connections as $name => $config) { $output->writeln(sprintf('Use connection named %s in %s environment.', $name, $this->getApplication()->getKernel()->getEnvironment())); $this->doSqlInsert($manager, $output, $name); } } } else { $output->writeln('You have to use --force to execute all SQL statements.'); } } protected function getSqlDir() { return sprintf('%s/propel/sql', $this->getApplication()->getKernel()->getRootDir()); } /** * @param \PropelSqlManager $manager * @param OutputInterface $output * @param string $connectionName */ protected function doSqlInsert(\PropelSqlManager $manager, OutputInterface $output, $connectionName) { try { $statusCode = $manager->insertSql($connectionName); } catch (\Exception $e) { return $this->writeSection( $output, array('[Propel] Exception', '', $e), 'fg=white;bg=red' ); } if (true === $statusCode) { $output->writeln('All SQL statements have been inserted.'); } else { $output->writeln('No SQL statements found.'); } } /** * @return array */ protected function getConnections() { $propelConfiguration = $this->getContainer()->get('propel.configuration'); $connections = array(); foreach ($propelConfiguration['datasources'] as $name => $config) { $connections[$name] = $config['connection']; } return $connections; } }