How to update data in controller?

If I have "Doe" in my field "name".

In my controller, how can I keep "Doe" and simply add "John" to the end?


public function actionUpdate($id)

	{

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


		// Uncomment the following line if AJAX validation is needed

		$this->performAjaxValidation($model);

   	

       

		if(isset($_POST['member']))

		{

     		                		

			if (!$model->name == 'Doe' ) {

	       $model->name = $model->name && 'John';

 		

	       }

	       


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

			

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('update',array(

			'model'=>$model,

		));

		}

Of course my above code doesn’t work. I want to do this via controller as well. Can anyone help?


$model->name = $model->name . 'John';

But this assignment, will be overwritten by


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

So maybe you need to add this your if statement, after line:


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

You just need to check out that from the model field a name value is equal to ‘Doe’ then you need to upend the string ‘John’ and nothing else.

this is a wrong part in your code




 if(isset($_POST['member']))

                {

                                                

                        if (!$model->name == 'Doe' ) {

               $model->name = $model->name && 'John';

                

               }






You need to do this




 if(isset($_POST['member']))

                {

                                                

                        if ($model->name == 'Doe' ) {

               $model->name = $model->name.' John '; //this will upend the 'john' string to the $model->name and will return $model->name='Doe John';

                

               }




Thank you. I knew I was close but not close enough!

And one more thing assignment of $model attribute should be before you actual Business logic.

Since you got all the form post data in $_POST.so to define your logic you should play with a $_POST value Like $name=$_POST[‘name’]." John";

and after that assign a $_POST value to $model attributes.

So, $model->name=$name; and so on…!!!

This will never confuse you and will work like a charm.

Alright thank you. I have already added a few other values. Works great.