Im having a weird issue. My employee table has id->int(11) as primary key. It also has code->varchar(36), password and some other fields. This code and password are the two fields employees use to login to the system. I used cakePHP form helper to build the login page with Code field and Password field; and things were going fine.
Today when i tried to update the employee table’s another field using $this->Model->save, it inserted a new row instead of updating. Thats when i learned that i need to set code as my primary key. So i put $primaryKey = 'code' in my Employee model.
The moment i put that, the Code field in my login page disappeared, leaving only Password field. When I removed the $primaryKey = 'code', the Code field came back.
So it has something to do with setting code as primary key in the Model. Is there any work around this?
I need to be able to update the Employee rows using code and retain my Code field in the login page.
It sounds like
codeis essentially a username field. Ifcodeis not defined as the primary key in your database, it is best not to set theprimaryKeyModel property ascode.I think most would recommend to use the primary key field
idto tell Cake which Employee record to update. But if you must usecode, try the following:Assuming your
codefield has a unique constraint in your database or is uniquely validated using Cake validation, you can usecodeto find the record you want to update. Once you have that record ($currentin the below case), you can then get the primary keyid, which is required soModel::saveknows which record to update.