cakephp
2011-09-08 49 views 0 likes 
0

我需要使用updateall更新部件的位置和列
在CakePHP updateAll需要幫助

if (!empty($column3[0])) { 
      $column = preg_split("/,/", $column3[0]); 
      $name = $column[2]; 
      switch ($name) { 
     case 'My Profile': 
        $table = 'WidgetProfile'; 
       break; 
       case 'My script': 
        $table = 'WidgetScript'; 
        break; 
       case 'My Setting': 
        $table = 'WidgetSetting'; 
        break; 
       case 'Upload Script': 
        $table = 'WidgetUpload'; 
        break; 
       case 'My message': 
        $table = 'WidgetMessages'; 
        break; 
       default : 
        echo "Error3 0"; 
      } 
      $this->loadModel($table); 
      $row = $this->$table->find('count',array('conditions'=>array('user_id'=>'$id)); 
      if($row==0){ 
      $this->$table->set(array('column',=> 3, 'position' => 1,'user_id'=>$id)); 
      $this->$table->save(); 
      }else{ 
      $this->$table->updateAll(**?????????????**); 

     } 

如何使用updateAll它

回答

1

這不是很從這個問題清楚你想要什麼要做,但它看起來像是在嘗試更新現有記錄,或者如果沒有記錄,請創建一個記錄。兩者都可以使用Model::save()。如果模型的ID被設置,它會更新,否則它會插入一個新的行。

$row = $this->$table->find(
    'first', 
    array(
     'conditions' => array('user_id'=> $id), 
     'fields' => "$table.id", 
     'recursive' => -1 
    ) 
); 

if(!empty($row)) { 
    $this->$table->id = $row[ $table ][ 'id' ]; 
} 

$this->$table->save(array('column' => 3, 'position' => 1, 'user_id'=> $id)); 
+0

如果(空($行)!){$ 這 - > $表 - > ID = $行[$表] [ 'ID']; } 用於 – taqman

+0

對不起,但我不明白你的意見。 – JJJ

+0

if(!empty($ row)){$ this - > $ table-> id = $ row [$ table] ['id']; } 請解釋 – taqman

相關問題