taqman
taqman

Reputation: 11

Need help in Cakephp updateAll

I need to use updateall to update position and column of widget

   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(**?????????????**);

        }

how to use updateAll for it

Upvotes: 0

Views: 934

Answers (1)

JJJ
JJJ

Reputation: 33163

It's not very clear from the question what you're trying to do, but it looks like you're trying to update an existing record, or if there isn't a record already, create one. You can use Model::save() for both. If the model's id is set it will update, otherwise it'll insert a new row.

$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 ) );

Upvotes: 1

Related Questions