2013-06-05 44 views
1

我在使用Yii框架的項目中構建數據訪問對象。其中一個插入查詢比較複雜,因爲它分佈在三個相關的表中。Yii中的SQL查詢

此時,我寫出了SQL查詢,並沒有使用QueryBuilder。

在插入功能的開始,我有

$connection = Yii::app()->db; 
$transaction = $connection->beginTransaction(); 

try { 
    $command = $connection->createCommand($this->insertQuestion); 

    //multiple $command->bindParam() calls 

按照documentation,一個CDbCommand實例可以重複使用,以建立多個查詢。但是,重新使用新查詢時必須調用CdbCommand::reset

這隻出現在文檔的QueryBuilder部分。由於我使用CdbCommand::bindParam綁定變量的查詢,而無需使用QueryBuilder的,是我有必要做

$command->reset(); 
$command->setText($sqlText); 
$command->bindParam("sqlVar", $variable, PDO::PARAM_INT); 

是否有可能在這種情況下使用CDbCommand::reset跳過?

回答

0

根據你可以做類似的文檔:

$transaction=$connection->beginTransaction(); 
try 
{ 
    $connection->createCommand($sql1)->execute(); 
    $connection->createCommand($sql2)->execute(); 
    //.... other SQL executions 
    $transaction->commit(); 
} 
catch(Exception $e) 
{ 
    $transaction->rollback(); 
} 

如果你知道你要使用的SQL,只是利用它來進行SQL1,$ SQL2和SQL3 $。