php 使用 Doctrine 2 转储数据库数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9071669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dump database data using Doctrine 2
提问by dextervip
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 with Bisna Doctrine 2 Integration?
是否可以使用学说 2 转储数据库?我已经读过 symfony 有一个库,它扩展了学说来做到这一点,但我如何在我的 zendframework 项目中使用它与 Bisna Doctrine 2 集成?
采纳答案by Julien
Doctrine has no database-dump feature. I agree it would be nice, but it's also not the ORM's goal.
Doctrine 没有数据库转储功能。我同意这会很好,但这也不是 ORM 的目标。
You could dump the database using
您可以使用转储数据库
- a PHP script
- a system mysqldump
- phpMyAdmin
- 一个 PHP 脚本
- 一个系统mysqldump
- phpMyAdmin
回答by Amit
For Symfony2:
对于 Symfony2:
Type
类型
php app/console doctrine:schema:create --dump-sql
in the command line
在命令行中
回答by Shakus
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 :)
这是一个旧线程,但我只是在 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
);
}
}
and AbstractCommand is just a class that extends symfony's ContainerAwareCommand:
而 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
{
}
回答by A.L
I created a small script that read the parameters from app/config/parameters.yml
and output all the data from a MySQL database to a file (with current datetime used as name).
我创建了一个小脚本,它app/config/parameters.yml
从 MySQL 数据库读取参数并将所有数据从 MySQL 数据库输出到文件(使用当前日期时间作为名称)。
Save this in the root of your Symfony project (e.g. mysqldump.sh
):
将其保存在 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 "$ 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
"))
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"
Result when running the script:
运行脚本时的结果:
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);
}
回答by GusDeCooL
Depend on your database. if you use mysql, create a php command to utilise mysqldump
取决于你的数据库。如果您使用 mysql,请创建一个 php 命令以使用mysqldump
like running thismysqldump -u YourUser -p YourDatabaseName > wantedsqlfile.sql
喜欢运行这个mysqldump -u YourUser -p YourDatabaseName > wantedsqlfile.sql
回答by Napche
For a more generic doctrine way:
对于更通用的教义方式:
##代码##