2012-12-05 56 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; 
}