user3894236
user3894236

Reputation: 114

how to update model in yii using specific attributes?

I have a model called DisnotificationUpdate. Primary key in the table is id.

public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'notifi_id' => 'Notifi',
            'view' => 'View',
            'userid' => 'Userid',
        );
    }

I want to update the table using userid attribute (eg: update DisnotificationUpdate set view='1' where userid='c01') How can I do this in yii?

Upvotes: 0

Views: 4031

Answers (2)

Think Different
Think Different

Reputation: 2815

Try this

  $models = DisnotificationUpdate::model()->findAllByAttributes(array('userid'=>'c01'));
  foreach($models as $model){
      $model->view = '1';
      $model->save();
  }

Or

  $sql = "UPDATE disnotificationupdate set view='1' where userid='c01'"; 
  //assuming you table name is disnotificationupdate 
  Yii::app()->bd->createCommand($sql)->execute();

Upvotes: 0

DisnotificationUpdate::model()->updateAll(array('view'=>1),'userid=:uid',array(':uid'=>$uid));

Upvotes: 4

Related Questions