当我使用名为 users_id
而不是 id
的自动增量主键时,如何删除 CakePHP 2.x 中的记录?
我正在使用以下代码:
$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();
关于cakephp - 如果主键不是 'id' ,如何删除记录? (CakePHP 2.x),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34354335/