2016-03-14 113 views
1

我使用CakePHP3.2.3編寫了一個平臺。我需要在特定服務器上創建一個數據庫和近100個表。我爲每個表創建了一個sql文件,並通過讀取sql文件並在其中執行sql來在遠程服務器上創建表。但是我從CakePHP DebugKit中得到了一些錯誤。它如下所示如何設置PDO :: MYSQL_ATTR_USE_BUFFERED_QUERY

Documentation API 
Error: SQLSTATE[HY000]: General error: 2014 Cannot execute queries  
while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). 
Alternatively, if your code is only ever going to run against mysql, you may 
enable query buffering by setting the 
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. 

我認爲fetchAll不適合我。但我不知道如何啓用與CakePHP連接的查詢緩衝。請告訴我如何製作它。

回答

0

我已經從CakePHP的源代碼中得到了答案。我們可以調用prepare函數,然後執行sql來啓用緩衝區。

$path = ROOT . DS . APP_DIR . DS . 'config' . DS . 'sql'; 
    $dir = new Folder($path); 
    $files = $dir->read(true); 
    $files = $files[1]; 
    foreach ($files as $file) { 
     $file = new File($dir->pwd() . DS . $file); 
     $sql = str_replace('%s', $tablePrefix, $file->read()); 

     $buffer = $connection->prepare($sql); 
     $buffer->execute(); 
    }