2012-12-05 57 views
0

我有一个SQL脚本,我打算在运行我的单元测试时用作数据夹具。我知道Doctrine DBAL有一个用于CLI的导入命令,但我只想在我的单元测试脚本中运行它。基于使用Doctrine导入执行SQL文件

\Doctrine\DBAL\Tools\Console\Command\ImportCommand 

我努力理解如何,我可以从PHP纯粹做到这一点,而无需使用EXEC如果我能帮助它

任何人都可以指向正确的方向吗?

感谢

回答

1

展望ImportCommand的代码,你可以提取它的重要组成部分,以创建自己的功能:

protected function executeSqlFile(Connection $conn, $file) 
{ 
    $sql = file_get_contents($file); 
    $lines = 0; 

    $stmt = $conn->prepare($sql); 
    $stmt->execute(); 

    do { 
     // Required due to "MySQL has gone away!" issue 
     $stmt->fetch(); 
     $stmt->closeCursor(); 

     $lines++; 
    } while ($stmt->nextRowset()); 

    return $lines; 
}