在 symfony2 控制器中调用 mysql 命令

Call mysql command in symfony2 controller

我有一个关于 symfony2 的问题。如何在控制器中执行 mysql 命令?像进口的东西。我尝试使用 execute() 但无济于事(我也没有收到任何错误): mysql -u 用户名--密码=密码 database_name < file.sql

获取 pdo 对象:$entityManager->getConnection()->getWrappedConnection()...

但已弃用。

否则,在学说中你有一个内置的基于 SQL 的语言 (DQL) 来创建类似 SQL 的请求和 returns 实体...请参阅文档:http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html

如果您想执行 shell 命令,我不明白您为什么要这样做...

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

$process = new Process('mysql -u username –-password=password database_name < file.sql');
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
   throw new ProcessFailedException($process);
}

http://symfony.com/doc/current/components/process.html#usage