使用Doctrine 2转储数据库数据

时间:2012-01-30 22:19:37

标签: php zend-framework symfony doctrine-orm

是否可以使用doctrine 2转储数据库?我已经读过symfony有一个扩展了doctrine的库,但我怎样才能在我的Zendframework项目中使用它与Bisna Doctrine 2 Integration?

6 个答案:

答案 0 :(得分:12)

对于Symfony2:

输入

php app/console doctrine:schema:create --dump-sql

在命令行中

答案 1 :(得分:7)

Doctrine没有数据库转储功能。 我同意它会很好,但它也不是ORM的目标。

您可以使用

转储数据库
  • 一个PHP脚本
  • 系统mysqldump
  • 成功运行

这是an article explaining those solutions

答案 2 :(得分:6)

我创建了一个小脚本,它从app/config/parameters.yml读取参数,并将MySQL数据库中的所有数据输出到一个文件(当前日期时间用作名称)。

将其保存在Symfony项目的根目录中(例如mysqldump.sh):

#!/bin/bash

# See http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in/23905052#23905052
ROOT=$(readlink -f $(dirname "$0"))

cd $ROOT

# Get database parameters
dbname=$(grep "database_name" ./app/config/parameters.yml | cut -d " " -f 6)
dbuser=$(grep "database_user" ./app/config/parameters.yml | cut -d " " -f 6)
dbpassword=$(grep "database_password" ./app/config/parameters.yml | cut -d " " -f 6)

filename="$(date '+%Y-%m-%d_%H-%M-%S').sql"

echo "Export $dbname database"

mysqldump -B "$dbname" -u "$dbuser" --password="$dbpassword" > "$filename"

echo "Output file :"

ls -lh "$filename"

运行脚本时的结果:

$ bash mysqldump.sh 
Export […] database
Warning: Using a password on the command line interface can be insecure.
Output file :
-rw-rw-r-- 1 […] […] 1,8M march   1 14:39 2016-03-01_14-39-08.sql

答案 3 :(得分:5)

这是一个旧线程,但我只是在Symfony做类似的事情,并决定为它开发一个实际的命令。这更像是一种Symfony方式,可以让您更好地控制输出,并允许您访问参数,因此您不必使用bash脚本解析Yaml:)

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('<comment>Dumping <fg=green>%s</fg=green> to <fg=green>%s</fg=green> </comment>', $this->database, $this->path ));
        $this->createDirectoryIfRequired();
        $this->dumpDatabase();
        $output->writeln('<comment>All done.</comment>');
    }

    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
        );
    }
}

和AbstractCommand只是一个扩展symfony的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
{
}

答案 4 :(得分:2)

取决于您的数据库。如果您使用mysql,请创建一个php命令以使用mysqldump

喜欢跑这个 mysqldump -u YourUser -p YourDatabaseName > wantedsqlfile.sql

答案 5 :(得分:0)

以更通用的学说方式:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $conn = $this->getDoctrineConnection('default');

    $path = $input->getArgument('filepath');
    if (! is_dir(dirname($path))) {
        $fs = new Filesystem();
        $fs->mkdir(dirname($path));
    }

    $cmd = sprintf('mysqldump -u %s --password=%s %s %s > %s',
        $conn->getUsername(),
        $conn->getPassword(),
        $conn->getDatabase(),
        implode(' ', ['variables', 'config']),
        $path
    );

    exec($cmd, $output, $exit_status);
}