当我使用名为users_id
而不是自动递增主键时,如何在 CakePHP 2.x 中删除记录id
?
我正在使用以下代码:
$this->User->delete(12);
但它不起作用。
当我使用名为users_id
而不是自动递增主键时,如何在 CakePHP 2.x 中删除记录id
?
我正在使用以下代码:
$this->User->delete(12);
但它不起作用。
您必须在模型中声明将用作主键的字段的名称。例如:
class User extends AppModel {
// user_id is the field name in the database
public $primaryKey = 'user_id';
}
现在应该可以使用以下方法:
$this->User->delete(12);
也允许使用此语法:
$this->User->id = 12;
$this->User>delete();
使用该deleteAll()
方法,您可以像在查询中那样指定条件。所以在你的情况下
$this->User->deleteAll(array('User.user_id' => 12));
有关详细信息,请参阅http://book.cakephp.org/2.0/en/models/deleting-data.html