Updatebypk Fields Keep Going Back To Default

Hi,

When I use updateByPK some of the DB field values I updated previously go back to the default, why is this happening?

James.

Can you post the relevant code?

Whoops, will post in a minute.

Strange. Can you turn on web logging and paste here the query that is being generated?

Are you doing any other updates in the same action?

Hi,

Here is the code …


Company::model()->updateByPK(Yii::app()->user->company_id, array("setup_company_settings" => "done"));

I have two fields in my company DB table "setup_company_settings" = "pending" and "setup_user_settings" = "done"

When I use the code to set the field "setup_company_settings" = "done" and "setup_user_settings" = "pending"

But why? Why is it not …

"setup_company_settings" = "done" and "setup_user_settings" = "done"

No one have any ideas on this one then?

All I want to do is update the record, which I tried to do, but other fields are going back to default when I try to update.

I can help more if you provide the information I asked for in my last post.

Hi,

I am doing another update in the same action, yes …




public function actionEdit() {

			

			$company = new Company;

			$company->isNewRecord = false;

			$company->id = Yii::app()->user->company_id;

			$company->updated_date = Yii::app()->global_function->formatDateSubmit(gmdate("Y-m-d H:i:s"));

			$company->updated_by_userid = Yii::app()->user->id;

			

			if(isset($_POST["Company"])) {

				

				$company->attributes = $_POST["Company"];

				

				if($company->validate()) {

					

					$company->save();

					// Company::model()->updateByPK(Yii::app()->user->company_id, array("setup_company_settings" => "done"));

					Yii::app()->user->setFlash("notice", "Success, the company has been saved.");

					$this->redirect(array("company/edit", "id" => $company->id));

					

				}

				

			} else {

				

				$company = $company->findByPK($company->id);

				

			}

			

			$view->models->company = $company;

			$this->render("edit", array("view" => $view));

			

		}



And if I comment out the updateByPK and just let it do the other update the problem still happens, so the problem is not caused by the updateByPK but by the save() method.

Can you see the problem from this code? I also know I can put an array of fields that I want it to update in the save() (or was it update()) method I cannot remember. Never the less, is there a way to do it without listing all the field values I want to update?

James.

Hi,

I solved it by doing …





public function actionEdit() {

			

			$company = Company::model()->findByPK(Yii::app()->user->company_id);

			$company->updated_date = Yii::app()->global_function->formatDateSubmit(gmdate("Y-m-d H:i:s"));

			$company->updated_by_userid = Yii::app()->user->id;

			

			if(isset($_POST["Company"])) {

				

				$company->attributes = $_POST["Company"];

				

				if($company->validate()) {

					

					$company->save();

					Company::model()->updateByPK(Yii::app()->user->company_id, array("setup_company_settings" => "done"));

					Yii::app()->user->setFlash("notice", "Success, the company has been saved.");

					$this->redirect(array("company/edit", "id" => $company->id));

					

				}

				

			}

			

			$view->models->company = $company;

			$this->render("edit", array("view" => $view));

			

		}




Company needed to be loaded etc.