【问题标题】:Error PDO Exception SQLSTATE[42000] when using CakeDC Users plugin使用 CakeDC 用户插件时出现错误 PDO 异常 SQLSTATE[42000]
【发布时间】:2013-04-07 06:47:11
【问题描述】:

我正在使用 cakephp 2.2.1 并尝试使用 cakedc 用户插件,当我尝试创建用户时出现以下错误,任何人都可以提供一些提示或建议来解决此错误 - 我已使用迁移插件。提前致谢 :)。

2013-04-07 06:31:33 Error: [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'register' at line 1
#0 C:\wamp\www\toppin\lib\Cake\Model\Datasource\DboSource.php(459): PDOStatement->execute(Array)
#1 C:\wamp\www\toppin\lib\Cake\Model\Datasource\DboSource.php(425): DboSource->_execute('register', Array)
#2 C:\wamp\www\toppin\lib\Cake\Model\Datasource\DboSource.php(669): DboSource->execute('register', Array, Array)
#3 C:\wamp\www\toppin\lib\Cake\Model\Datasource\DboSource.php(611): DboSource->fetchAll('register', Array, Array)
#4 C:\wamp\www\toppin\lib\Cake\Model\Model.php(788): DboSource->query('register', Array, Object(User))
#5 C:\wamp\www\toppin\app\Plugin\Users\Controller\UsersController.php(331): Model->__call('register', Array)
#6 C:\wamp\www\toppin\app\Plugin\Users\Controller\UsersController.php(331): User->register(Array)
#7 [internal function]: UsersController->add()
#8 C:\wamp\www\toppin\lib\Cake\Controller\Controller.php(485): ReflectionMethod->invokeArgs(Object(UsersController), Array)
#9 C:\wamp\www\toppin\lib\Cake\Routing\Dispatcher.php(186): Controller->invokeAction(Object(CakeRequest))
#10 C:\wamp\www\toppin\lib\Cake\Routing\Dispatcher.php(161): Dispatcher->_invoke(Object(UsersController), Object(CakeRequest), Object(CakeResponse))
#11 C:\wamp\www\toppin\app\webroot\index.php(92): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#12 {main}

添加功能:

public function add() {
        if ($this->Auth->user()) {
            $this->Session->setFlash(__d('users', 'You are already registered and logged in!'));
            $this->redirect('/');
        }

        if (!empty($this->request->data)) {
            $user = $this->User->register($this->request->data);
            if ($user !== false) {
                $this->_sendVerificationEmail($this->User->data);
                $this->Session->setFlash(__d('users', 'Your account has been created. You should receive an e-mail shortly to authenticate your account. Once validated you will be able to login.'));
                $this->redirect(array('action' => 'login'));
            } else {
                unset($this->request->data[$this->modelClass]['password']);
                unset($this->request->data[$this->modelClass]['temppassword']);
                $this->Session->setFlash(__d('users', 'Your account could not be created. Please, try again.'), 'default', array('class' => 'message warning'));
            }
        }
    }

【问题讨论】:

  • 你在 add() 中有什么
  • 我已经用插件用户控制器的添加操作更新了这篇文章

标签: cakephp cakephp-2.0 cakephp-2.1 cakephp-model cakedc


【解决方案1】:

由于某种原因,您的 User 模型没有被 CakePHP 使用,或者 register() 方法不存在。 CakePHP 会将不存在的方法作为 SQL 语句执行。如果没有使用 Model,CakePHP 将根据您的 AppModel

自动创建一个模型

要发现User 模型是否找不到,试试这个;

public function add()
{
    debug(get_class($this->User));

    // ....
}

这应该输出User。如果它输出AppModelModel,那么CakePHP 使用的是通用模型,这表明它找不到您的用户模型。

  • 清除您的 app/tmp/cache/modelsapp/tmp/cache/persistent 目录,或者如果您使用 APC,请清除用户缓存。
  • 如果您在控制器中使用多个模型,请检查 User 模型是否在您的 $uses 数组中
  • 检查您的用户模型是否在此文件中:app/Model/User.php 并且类名是 User(单数,而不是复数

另一方面,如果您的模型正确加载(调试示例显示User,而不是AppModel),然后检查您是否真的有register() 方法,如果不是,你应该创建 if。

希望这会有所帮助。

[更新]

刚刚意识到您正在使用用户插件,并且可能正在加载错误用户模型。使用插件时,您还需要使用 plugin 中的User 模型,而不是您自己的User 模型。

如果您确实拥有自己的用户模型,则应将其重命名为 User 以外的名称(例如 AppUser),以防止 CakePHP 加载错误的模型。按照插件文档中关于如何在此处扩展插件“组件”的说明:extending the Model

但是,如果您想从插件扩展 User 模型,您还需要扩展插件的 UsersController,请参阅 Extending the Controller

如果您需要扩展插件模型和控制器,最好从您的应用程序中删除它们(例如app/Model/User.phpapp/Controller/UsersController.php

我保留了我最初的建议,因为它们在其他情况下仍然可能会有所帮助

【讨论】:

    【解决方案2】:

    你应该对照用户表检查$this->request->data中的内容,也许有些值是不合法的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多