Update field

Stock Controller:

public function actionUpdateStatus() {




Stock Model:

public static function updatestatus() {


		Yii::app()->db->createCommand('UPDATE stock SET status=1 WHERE id=42')->execute();




<button type="button" class="btn btn-xs yellow">

                               <a style="color:white; text-decoration: none !important;" href="<?php echo Yii::app()->createUrl('/stock/UpdateStatus').'&id='.$model->id;?>"></i>JOB COMPLETE</button></a>


I tried using $this->id to substitute 42 but it gives me an error. I am only using 42 for testing purposes.

I have tried a couple of solutions but I am still stuck with errors.

Any solution is greatly appreciated.

Use updateAll method to update directly your model, not need to write sql query.

eg. -

 stock::model()->updateAll(array("Status" => 1), "id = ".$id);

Hi Rohit,

I am only updating a specific record to be precise. How do I pass the id?

Upon a user clicking the button, the particular record will switch its status from 0 to 1.

With the help of ajaxlink() method you can pass id through ajax.

Try this -

Create a button using ajaxlink() method in your view -

eg. -

echo CHtml::ajaxlink('Update', array('stock/updateStatus'), array(

                "type" => "POST",


                "data" => array("id" => $id, "ajax" => true),

                "success" => "function(data){




In your Stock Controller -

eg. -

public function actionUpdateStatus() {



      $id = $_REQUEST['id'];


      stock::model()->updateAll(array("Status" => 1), "id = ".$id);

      echo 'Active';   //Send text as you want to print



for more help - http://www.yiiframework.com/wiki/48/by-example-chtml/#hh0

Having any query, leave a comment.

Thank you for your suggestion.

I ended up using the code below.

public function actionUpdateStatus($id) {



		$model->id = $id;





		            'status'=>new CDbExpression('1'),








		//echo $id;