Update action on Models

How do I update an existing record on a model?

A further note: I noticed that when viewing the demo’s the action for “updating” records it appears to be the same as the “create” action. What I mean is that they both use the “save” method, and what I find really strange is that there does not appear to be the “id” set in any of this. Generally when editing records in other applications there is always an “id” set so that the database knows which record to edit. But I have not seen this in Yii, is this true? How does the “id” get set when editing records?

Anyone have any ideas on this, all I am saying is that when updating a record in the database the model does not appear to need an "ID" is this correct?

What are you talking about man, the id is usually in the URL as a GET parameter. Can you post the URL of an update action please so it can be confirmed.

Mr. Barnsley, please! :)

Could at least create a out-of-the-box Yii project from scratch?

Maybe create a couple of simple models and CRUDs using Gii ?

Study the fairly simple code it generates.

Most of your questions will be answered by that procedure.

I was about to explain it when I realised that it’s totally obvious from looking at the code:

actionCreate and actionUpdate in the controller and the corresponding views: create.php, update.php and _form.php.

Good luck! :)

Hint: primary id is generated by the database when a model is saved.

When you update a model you use the id of the model you are updating. Take a look at the code - it does not bite. :P

Yeah, it should definitely require an id.

In the default generated crud it gets the id from the _GET array when loadModel() is called at the beginning of the function - perhaps the loadModel function is what you need to take a look at to see how it ties together?

Thanks, so it gets it from GET that was all I was asking.

I was not asking how to update a model. I was asking why Yii does not appear to need an "ID" when updating, because its just $model->save();

As you can see, it does not need you to set the ID, so I was just wondering how "Yii" does this automatically. As you have already this, by using GET then it does not matter now.




public function actionUpdate()

	{

		$model=$this->loadModel();

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

		{

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

			if($model->save())

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

		}


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

			'model'=>$model,

		));

	}



Look in the controller::loadModel function. :)

Gii generates this kind of code, FYI:


	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */

	public function actionUpdate($id)

	{

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


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

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

			if($model->save())

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

		}


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

			'model'=>$model,

		));

	}



It does indeed take a parameter (GET).

But, of course, it will also work without. If the loadModel checks for $_GET[‘id’].

The Yii book uses this code in loadModel:


/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 */

	public function loadModel()

	{

		if($this->_model===null)

		{

			if(isset($_GET['id']))

				$this->_model=Project::model()->findbyPk($_GET['id']);

			if($this->_model===null)

				throw new CHttpException(404,'The requested page does not exist.');

		}

		return $this->_model;

	}



And none of the actions takes parameters.

It was written against an older Yii which didn’t have automatic parameter binding.

@JamesBarnsley: What version of Yii are you using?

Latest version, and the code was taken from Blog, Post Controller demo application.