Yii Framework Forum: I Have Problem To Updat Just One Filed Using Save() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

I Have Problem To Updat Just One Filed Using Save() $user->u_prof->save(true,array('note')); saves al Rate Topic: -----

#1 User is offline   amiri 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 08-January 12

Posted 28 February 2013 - 08:20 AM

Hello all

Sometimes we want to update just one or some fields of table, instead of update all fields.
I have trouble with the following syntax! Since it updates all loaded fields from table in the DB.

	/**
	 * Shows the Control panel of User
	 */
	public function actionIndex()
	{
		$user=$this->loadUser(Yii::app()->user->id); // Loads $user & $user->u_prof tables fron DB
		if (empty($user->u_prof))
			throw new CHttpException(403,'The user has not created profile yet.');
		
		// Save UserProfile->note value from forms
		if (isset($_POST['UserProfile'])) { // save $user->u_prof->note
			$user->u_prof->attributes= $_POST['UserProfile'];
			$user->u_prof->save(true,array('note'));
			}
			
		$this->render('index',array(
			'user'=>$user,
		));
	}


also the code below doesn't work too, seems the syntax is not correct!
		// Save $user->u_prof->note values received from a form by post method
		// u_prof is a relation defigned in User model, point to UserProfile model.
		if (isset($_POST['UserProfile'])) {
			$user->u_prof->update (array(
				'condition'=>'uid='.Yii::app()->user->id,
				array('note'=>':note'),
				'params' => array(':note'=>$_POST['UserProfile']['note'),
				));
			}


Note: i wanna update only one field '$user->u_prof->note' in database, while it is filtered against SQL injection. you see i have tried to use
'params' => array(':note'=>$_POST['UserProfile']['note']


i'll be glad if some body writes the correct syntax to save ONLY 'note' field of table in the DB.
0

#2 User is offline   amiri 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 70
  • Joined: 08-January 12

Posted 03 March 2013 - 09:01 AM

no body wanna help me?!
0

#3 User is online   Balu 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 152
  • Joined: 09-August 11
  • Location:Kerala, India

Posted 03 March 2013 - 10:05 AM

View Postamiri, on 28 February 2013 - 08:20 AM, said:

Hello all

Sometimes we want to update just one or some fields of table, instead of update all fields.
I have trouble with the following syntax! Since it updates all loaded fields from table in the DB.

	/**
	 * Shows the Control panel of User
	 */
	public function actionIndex()
	{
		$user=$this->loadUser(Yii::app()->user->id); // Loads $user & $user->u_prof tables fron DB
		if (empty($user->u_prof))
			throw new CHttpException(403,'The user has not created profile yet.');
		
		// Save UserProfile->note value from forms
		if (isset($_POST['UserProfile'])) { // save $user->u_prof->note
			$user->u_prof->attributes= $_POST['UserProfile'];
			$user->u_prof->save(true,array('note'));
			}
			
		$this->render('index',array(
			'user'=>$user,
		));
	}




also the code below doesn't work too, seems the syntax is not correct!
		// Save $user->u_prof->note values received from a form by post method
		// u_prof is a relation defigned in User model, point to UserProfile model.
		if (isset($_POST['UserProfile'])) {
			$user->u_prof->update (array(
				'condition'=>'uid='.Yii::app()->user->id,
				array('note'=>':note'),
				'params' => array(':note'=>$_POST['UserProfile']['note'),
				));
			}


Note: i wanna update only one field '$user->u_prof->note' in database, while it is filtered against SQL injection. you see i have tried to use
'params' => array(':note'=>$_POST['UserProfile']['note']


i'll be glad if some body writes the correct syntax to save ONLY 'note' field of table in the DB.


Hey use this code this will work for you

$model_1 =User::model()->findByAttributes(array('uid='.Yii::app()->user->id));

$model_1->note  = $_POST['UserProfile']['note'];
$model_1->save(false);


I hope this will work for you
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users