Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP putting `id` = '1' in WHERE clause

Tags:

php

mysql

cakephp

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.


Final update: problem solved

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.

like image 811
Renato Dinhani Avatar asked Dec 05 '25 06:12

Renato Dinhani


1 Answers

Try this instead:

$this->request->data['Administrator']['id'] = $id; // <-- Line changed
$this->Administrator->save($this->request->data);
like image 114
472084 Avatar answered Dec 07 '25 20:12

472084



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!