如果主键未命名为“id”,如何删除记录?(CakePHP 2.x)
当我使用名为users\u id而不是id的自动递增主键时,如何删除CakePHP 2.x中的记录 我正在使用以下代码:如果主键未命名为“id”,如何删除记录?(CakePHP 2.x),cakephp,cakephp-2.x,Cakephp,Cakephp 2.x,当我使用名为users\u 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);
但是它不起作用。您必须在模型中声明将用作主键的字段的名称。例如:
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();
使用deleteAllmethod,您可以像在查询中那样指定条件。
所以在你的情况下是这样的
$this->User->deleteAll(array('User.user_id' => 12));
有关更多详细信息,请参阅哪种版本的蛋糕?你有什么错误?您如何设置您的模型?您能否解释一下cakephp中的用户、findByEmail和访问者是什么$this->User->Visitor->findbyemail返回括号中指定项的cakephp findBy函数,例如$this->User->VisitorfindByEmail'test@email.com'; 返回带有括号中指定电子邮件的访问者。这只是$this->User->Visitor->find'all',array'conditions'=>array'Visitor.email'=>的一个简短版本test@email.com'; 见: