使用原则 2 转储数据库数据
2022-08-31 00:33:30
是否可以使用原则 2 转储数据库?我已经读到symfony有一个扩展教义的库来做到这一点,但是我怎么能在我的zendframework项目中使用它与Bisna Doctrine 2集成?
是否可以使用原则 2 转储数据库?我已经读到symfony有一个扩展教义的库来做到这一点,但是我怎么能在我的zendframework项目中使用它与Bisna Doctrine 2集成?
对于 Symfony2:
类型
php app/console doctrine:schema:create --dump-sql
在命令行中
这是一个旧的线程,但我只是在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
{
}