I'm trying to update a row with the following command, but CakePHP always put a '1'
in the WHERE
clause.
# the PHP command
$this->Administrator->id = $id; # id = 6, it wasn't changed
$this->Administrator->save($this->request->data);
-- simplified version of the generated SQL
UPDATE `someTable` SET `someField` = 'value', `id` = '6' WHERE `id` = '1'
It's interesting that the id
is correct in the generated SQL in the update part, but not in the WHERE
clause, and yes, the id
column is the primary key in the table. I'm using the last version of CakePHP.
What I'm doing wrong? How the id
in the WHERE
clause can be 6
?
EDIT: It produces the same result with the generated code with Bake. Maybe a problem in the model or database?
EDIT 2: I'm logging all the queries and before trying to update, CakePHP perform some SELECT count(*)
and it uses the WHERE id = 6
.
EDIT 3: Insert, delete and read works fine.
I'm not sure what happened, but I think that I solved the problem.
First, the update wasn't working event with the code generated with Bake.
Second, only the update of Administrators aren't working, all the others are ok.
Third, I think the problem came from a column in the administrators
table that is also called administrators
and it is a TINYINT(1)
. I changed the column name to administrators_area
, changed some code too and all worked.
I think this column was creating some conflict and because this, the UPDATE wasn't working. Before trying this, I changed the table name to users
keeping the column as administrators
and worked too.
If the problem wasn't a conflict between table name and table column with the same name, it was a great coincidence that the problem solved after I changed the name.
Try this instead:
$this->request->data['Administrator']['id'] = $id; // <-- Line changed
$this->Administrator->save($this->request->data);
Are you building this submission from a form? What do you have in your form on the View? My first guess is that you are neglecting to include the hidden input for id that tells CakePHP what Model object we are updating:
echo $this->Form->input('id',array('type' => 'hidden'));
What sort of data are you receiving if you call these functions?
debug($this->request->data);
debug($this->Administrator);
If its not an issue with the form itself I would be suspicious only because they ID=1 should really only come from some residual data or an unset model.