Update field

Stock Controller:


public function actionUpdateStatus() {

	

		Stock::model()->updatestatus();


	}

Stock Model:


public static function updatestatus() {

	

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

		

		}

Button


<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>

</div>

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",

				"dataType"=>"html",

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

                "success" => "function(data){

                                 jQuery('#your_id').html(data);

                             }",

                    ));



In your Stock Controller -

eg. -


public function actionUpdateStatus() {


    if(Yii::app()->request->isAjaxRequest){

    

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

	

		//Stock::model()->updatestatus();

		$model->id = $id;

		

		Yii::app()->db->createCommand()

		    ->update('stock', 

		        array(

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

		        ),

		        'id=:id',

		        array(':id'=>$model->id)

		    );

		

		$this->redirect(array('admin'));

		

		//echo $id;

	}