Cake php SQLSTATE [42000]:语法错误或访问冲突:1064

时间:2014-09-05 09:14:16

标签: php mysql cakephp

添加用户时出现此错误。

我有一个视图 adduser.ctp

在用户控制器中我有这个功能

 public function addUser() {
    $userGroups = $this->UserGroup->getGroups();
    unset($userGroups[1]);
    unset($userGroups[2]);
    $this->set('userGroups', $userGroups);
    if ($this->request->isPost()) {
        $this->User->set($this->data);
        if ($this->User->AddEditValidate()) {
            $this->request->data['User']['email_verified'] = 1;
            $this->request->data['User']['active'] = 2;
            $this->request->data['User']['parent_id'] = $this->Session->read('logged_client_id');
            $salt = $this->UserAuth->makeSalt();
            $this->request->data['User']['salt'] = $salt;
            $this->request->data['User']['temp_password'] = $this->request->data['User']['password'];
            $this->request->data['User']['password'] = $this->UserAuth->makePassword($this->request->data['User']['password'], $salt);
            if ($this->User->save($this->request->data, false)) {
                $this->sendLoginEmail($this->request->data['User']);
                $this->Session->setFlash(__('The user is successfully added'), 'success_flash');
                $this->redirect('/clientUsers/' . $this->Session->read('logged_client_id'));
            } else {
                $this->Session->setFlash(__('Problem in saving user information'), 'error_flash');
            }
        }
    }
}

现在在用户模型中,我通过以下函数进行验证

function AddEditValidate() {
    $validate1 = array(
        "user_group_id" => array(
            'rule' => array('comparison', '!=', 0),
            'message' => 'Please select group'),
        'first_name' => array(
            'mustNotEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Please enter first name'),
            'custom' => array(
                'rule' => 'alphaNumericSpace',
                'message' => 'First name should be alphnumeric')
        ),
        'last_name' => array(
            'mustNotEmpty' => array(
                'rule' => 'notEmpty',
                'on' => 'create',
                'message' => 'Please enter last name'),
            'custom' => array(
                'rule' => 'alphaNumericSpace',
                'message' => 'Last name should be alphnumeric')
        ),
        'email' => array(
            'mustNotEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Please enter email',
                'last' => true),
            'mustBeEmail' => array(
                'rule' => array('email'),
                'message' => 'Please enter valid email',
                'last' => true),
            'mustUnique' => array(
                'rule' => 'isEmailUnique',
                'message' => 'This email is already registered',
            )
        ),
        'oldpassword' => array(
            'mustNotEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Please enter old password',
                'last' => true),
            'mustMatch' => array(
                'rule' => array('verifyOldPass'),
                'message' => 'Please enter correct old password'),
        ),
        'password' => array(
            'mustNotEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'Please enter password',
                'on' => 'create',
                'last' => true),
            'complex' => array(
                'rule' => array('complexPassword'),
                'message' => 'Password should be six characters long, which should have minimum one alphabet, one number and one special character',
            ),
        ),
        'captcha' => array(
            'mustMatch' => array(
                'rule' => array('recaptchaValidate'),
                'message' => ''),
        )
    );
    $this->validate = $validate1;
    return $this->validates();
}

它给了我错误

  

错误:SQLSTATE [42000]:语法错误或访问冲突:1064您   您的SQL语法有错误;检查对应的手册   您的MySQL服务器版本,以便在附近使用正确的语法   ' AddEditValidate'在第1行

     

SQL查询:AddEditValidate

请帮帮我。

由于

1 个答案:

答案 0 :(得分:0)

版本3.0中未引用数据库列名称。您在字段名称中使用SQL关键字。如果您无法重命名列,则应启用标识符引用。

来自book.cakephp.org

$conn->driver()->autoQuoting(true);
  

默认情况下,CakePHP不会在生成的SQL查询中引用标识符。原因是标识符引用有一些缺点:

     
      
  • 性能开销 - 引用标识符比不执行它要慢得多且复杂。
  •   
  • 在大多数情况下不需要 - 在遵循CakePHP惯例的非遗留数据库中,没有理由引用标识符。
  •   
     

如果您使用的是需要标识符引用的旧架构,则可以使用Configuration中的quoteIdentifiers设置启用它。您还可以在运行时启用此功能:

     

启用后,标识符引用将导致额外的查询遍历,将所有标识符转换为IdentifierExpression对象。