public function primaryKey() {
return array('id_prova'=>$this->id_prova,'id_turno'=>$this->id_turno,'data'=>$this->data,'ano_letivo'=>$this->ano_letivo);
}
My problem is that I have a "editable" primary key of type varchar(9). I have added the key column to the form and the safeAttributes in the model. But when i call save() and see the query done in the database i see something like following:
UPDATE `tbproject` SET `ID`='mynewkey', ... other columns... WHERE `tbproject`.`ID`='mynewkey'
where ‘mynewkey’ is the value entered on the form. The problem is that due to the form post I overwrite the “old” value of my key which is in the database.
And therefore it doesn’t update anything cause the wrong key is used in the where part.
the correct sql would be
UPDATE `tbproject` SET `ID`='mynewkey', ... other columns... WHERE `tbproject`.`ID`='myoldkey'
Does someone know how to solve this or show me the right way to change the primary key value of a database entry?
Wow, reading all this, it seems to be a good idea to use auto incremented integers as id.
I was just about to modify the CRUD templates, because I realized I wasn’t able to insert new objects that used a varchar as id. Now that I see that there are so much other related problems, I think I will modify my db schema.
At the end I discovered the problem I had was not due to yii framework but in the way I accessed the $_POST[] variable, I was trying to insert a NULL value as a primary key!!!
I put the code listed below in my controller and everything works perfectly
$va = new va;
$va->attributes=$_POST['va'];//CORRECT WAY
//$va->setAttribute("id", $_POST["va_id"]);//WRONG WAY
//$va->setAttribute("vqIq", $_POST["va_vqId"]);//WRONG WAY
$va->save();