updateAll() function not working proper on update record

I have implement following code :

model code:



public function rules()


    {


        return [


          -------------------


	    ['message_of_day_active', 'integer'],


            [['creation_date', 'message_of_day_active'], 'safe'],


         ----------------------------------------


        ];


    }


controller code:



$model = $this->findModel($id);





	if ($model->load(Yii::$app->request->post())) {


		$model->attributes = $_POST['MessageOfDay'];


		$model->creation_date= new \yii\db\Expression('NOW()');


		if($_POST['MessageOfDay']['message_of_day_active']==1) {


			$model->updateAll(['message_of_day_active'=>0], 'message_of_day_active =1');


			$model->message_of_day_active =1;


		}


		if($model->save())


			return $this->redirect(['view', 'id' => $model->id]);


	} 


		return $this->render('update', [


			'model' => $model,


			]);


View Code:



<?= $form->field($model, 'message_of_day_active')->checkBox(['value'=>1, 'uncheckValue'=>0], ['template' => "{input} <span class='status'> </span>"]); ?>


when i have update record at time check box is selected but other record is updated but message_of_day_active field is set 0

when am change code for if condition is



if($_POST['MessageOfDay']['message_of_day_active']===1) {





--------------


}


at time record message_of_day_active field is set 1 update but updateAll() it not set 0 to all record

i have set message_of_day_active int(3) datatype

i have use PHP 5.5.19

Note: I have same code is use in yii 1.1.14 it work proper but yii 2.0.1 it not work.

What is problem in this code???

It is Yii2 Bug?..

please check this problem

If you migrate to Yii2, please leave your knowledge about Yii1 in your back. Yii2 is a different framework.

ActiveRecord::updateAll() is a static method. its update directly to database and not affected to ActiveRecord instance.

You are wrong when using checkbox doc.

BTT. You want only one row that has value message_of_day_active=1, right?

So, before save, you set all records to 0.

How about this




if ($model->load(Yii::$app->request->post())) {

    $model->creation_date= new \yii\db\Expression('NOW()');    

    if($model->message_of_day_active ==1){

        MessageOfDay::updateAll(['message_of_day_active'=>0],['message_of_day_active'=>1]);

    }

    if($model->save())

        return $this->redirect(['view', 'id' => $model->id]);

    } 

}


return $this->render('update', [

'model' => $model,

]);