How do I delete a record in CakePHP 2.x when I am using an auto increment primary key named users_id
instead of id
?
I am using the following code:
$this->User->delete(12);
But it is not working.
How do I delete a record in CakePHP 2.x when I am using an auto increment primary key named users_id
instead of id
?
I am using the following code:
$this->User->delete(12);
But it is not working.
You have to declare in your model the name of the field you will be using as primary key. For example:
class User extends AppModel {
// user_id is the field name in the database
public $primaryKey = 'user_id';
}
The following should now work:
$this->User->delete(12);
This syntax is also allowed:
$this->User->id = 12;
$this->User>delete();
With the deleteAll()
method you can specifiy conditions like you would do in a query.
So in your case it would be
$this->User->deleteAll(array('User.user_id' => 12));
refer to http://book.cakephp.org/2.0/en/models/deleting-data.html for more details