2014-04-25 70 views
0

有人可以幫我把下面的代碼轉換成Yii查詢嗎?我會liek它返回款型的陣列派生列如何將PDO查詢轉換爲Yii查詢?

$sql = 'UPDATE jobs 
    RIGHT JOIN (
     SELECT jobs.JOBNO, 
      round(details' . $type['type'] . '.' . $type['km'] . ' * sum(PRICE),2) AS JOBSVALUE 
     FROM jobs 
     JOIN projects ON jobs.PROJID = projects.PROJID 
     JOIN biditems ON projects.id = biditems.project_id 
     JOIN details' . $type['type'] . ' on jobs.JOBNO = details' . $type['type'] . '.JOBNO 
     WHERE jobs.PROJID = :pid 
     GROUP BY jobs.JOBNO 
    ) AS temp ON jobs.JOBNO = temp.JOBNO 
    SET jobs.VALUE = JOBSVALUE'; 

$command=$connection->createCommand($sql); 
$command->bindValue(":pid", $model->PROJID,PDO::PARAM_INT); 
$command->execute(); 


$sql = "UPDATE jobs j 
    JOIN (
     SELECT j.JOBNO, COUNT(l.JOBNO) AS numlis 
     FROM lineitems l 
     RIGHT JOIN jobs j ON j.JOBNO = l.JOBNO 
     WHERE j.PROJID = :pid 
     GROUP BY j.JOBNO 
    ) t ON j.JOBNO = t.JOBNO 
    SET `VALUE` = 0, `EARNED` = 0 
    WHERE PROJID = :pid AND t.numlis = 0;"; 

$command=$connection->createCommand($sql); 
$command->bindValue(":pid", $model->PROJID,PDO::PARAM_INT); 
$command->execute(); 

1日嘗試

$sql = "select jobs.JOBNO, round(details".$type['type'].".".$type['km']." * sum(PRICE),2) AS JOBSVALUE 
       from jobs 
       join projects on jobs.PROJID = projects.PROJID 
       join biditems on projects.id = biditems.project_id 
       join details".$type['type']." on jobs.JOBNO = details".$type['type'].".JOBNO 
       where jobs.PROJID = :pid 
       GROUP BY jobs.JOBNO";      
     $command=$connection->createCommand($sql); 
     $command->bindValue(":pid",$model->PROJID,PDO::PARAM_INT); 
     $result = $command->queryAll(); 
     foreach ($result as $value) { 
      $job = Jobs::model()->findByPk($value['JOBNO']); 
      $job->VALUE = $value['JOBSVALUE']; 
      $job->save(); 
     } 

$sql = "SELECT j.JOBNO, COUNT(l.JOBNO) AS numlis 
        FROM lineitems l 
        RIGHT JOIN jobs j ON j.JOBNO = l.JOBNO 
        WHERE j.PROJID = :pid 
        GROUP BY j.JOBNO"; 
     $command=$connection->createCommand($sql); 
     $command->bindValue(":pid",$model->PROJID,PDO::PARAM_INT); 
     $result = $command->queryAll(); 
     foreach ($result as $value) { 
      if($value['numlis'] == 0){ 
       $job = Jobs::model()->findByPk($value['JOBNO']);    
       $job->VALUE = 0; 
       $job->EARNED = 0;   
       $job->save(); 
      } 
     } 
+0

查看http://www.yiiframework .com/doc/api/1.1/CDbCommand – Dinistro

+1

您會首先添加您的嘗試嗎?人們會提供幫助,但前提是他們看到事先的努力。 – halfer

+0

@halfer更新 – shorif2000

回答

1

只是字面上以下的文檔:

$job = Yii:app()->db 
    ->createCommand() 
    ->select(
       'jobs.JOBNO, round(details'.$type['type'].'.'.$type['km'].' * sum(PRICE), 2)' 
      ) 
    ->join('projects', 'jobs.PROJID = projects.PROJID') 
    ->join('biditems', 'projects.id = biditems.project_id') 
    ->join('details'.$type['type'], 'jobs.JOBNO = details'.$type['type'].'.JOBNO') 
    ->where('jobs.PROJID=:pid', array(':pid' = $model->PROJID)) 
    ->group('jobs.JOBNO') 
    ->queryRow(); 
+0

' - > where('jobs.PROJID =:pid',array(':jobno'= $ model-> PROJID))'。它不應該是':jobno'或':pid'而不是兩個? – h2ooooooo

+0

你忘了我認爲的更新 – Dinistro