Dump database data using Doctrine 2

后端 未结 6 2132
萌比男神i
萌比男神i 2021-01-01 21:50

Is it possible to dump a database using doctrine 2? I have read that symfony has a library which extends doctrine to do it but How could I use it in my zendframework project

6条回答
  •  醉梦人生
    2021-01-01 22:32

    This is an old thread but I was just doing something similar in Symfony and decided to develop an actual command for it. That's more of a Symfony way of doing it and gives you more control on the output as well as allowing you access to the parameters, so you don't have to parse Yaml using bash script :)

    namespace Fancy\Command;
    
    use Fancy\Command\AbstractCommand;
    use Symfony\Component\Console\Input\InputArgument;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Input\InputOption;
    use Symfony\Component\Console\Output\OutputInterface;
    use Symfony\Component\Filesystem\Filesystem;
    use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
    
    class DatabaseDumpCommand extends AbstractCommand
    {
    
        /** @var OutputInterface */
        private $output;
    
        /** @var InputInterface */
        private $input;
    
    
        private $database;
        private $username;
        private $password;
        private $path;
    
        /** filesystem utility */
        private $fs;
    
        protected function configure()
        {
            $this->setName('fancy-pants:database:dump')
                ->setDescription('Dump database.')
                ->addArgument('file', InputArgument::REQUIRED, 'Absolute path for the file you need to dump database to.');
        }
    
        /**
         * @param InputInterface $input
         * @param OutputInterface $output
         * @return int|null|void
         */
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            $this->output = $output;
            $this->database = $this->getContainer()->getParameter('database_name') ; 
            $this->username = $this->getContainer()->getParameter('database_user') ; 
            $this->password = $this->getContainer()->getParameter('database_password') ; 
            $this->path = $input->getArgument('file') ; 
            $this->fs = new Filesystem() ; 
            $this->output->writeln(sprintf('Dumping %s to %s ', $this->database, $this->path ));
            $this->createDirectoryIfRequired();
            $this->dumpDatabase();
            $output->writeln('All done.');
        }
    
        private function createDirectoryIfRequired() {
            if (! $this->fs->exists($this->path)){
                $this->fs->mkdir(dirname($this->path));
            }
        }
    
        private function dumpDatabase()
        {
            $cmd = sprintf('mysqldump -B %s -u %s --password=%s' // > %s'
                , $this->database
                , $this->username
                , $this->password
            );
    
            $result = $this->runCommand($cmd);
    
            if($result['exit_status'] > 0) {
                throw new \Exception('Could not dump database: ' . var_export($result['output'], true));
            }
    
            $this->fs->dumpFile($this->path, $result); 
        }
    
        /**
         * Runs a system command, returns the output, what more do you NEED?
         *
         * @param $command
         * @param $streamOutput
         * @param $outputInterface mixed
         * @return array
         */
        protected function runCommand($command)
        {
            $command .=" >&1";
            exec($command, $output, $exit_status);
            return array(
                  "output"      => $output
                , "exit_status" => $exit_status
            );
        }
    }
    

    and AbstractCommand is just a class that extends symfony's ContainerAwareCommand:

    namespace Fancy\Command;
    
    use Symfony\Component\HttpFoundation\Request;
    
    use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
    use Symfony\Component\Console\Input\InputArgument;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Input\InputOption;
    use Symfony\Component\Console\Output\OutputInterface;
    
    abstract class AbstractCommand extends ContainerAwareCommand
    {
    }
    

提交回复
热议问题