错误PDO异常SQLSTATE [42000]使用CakeDC Users插件时 [英] Error PDO Exception SQLSTATE[42000] when using CakeDC Users plugin

查看:268
本文介绍了错误PDO异常SQLSTATE [42000]使用CakeDC Users插件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iam使用cakephp 2.2.1并试图使用cakedc用户插件,当我尝试创建一个用户我得到以下错误,任何人都可以给一些提示或建议来解决这个错误 - 我已经加载所有表使用迁移插件。提前致谢 :)。

  2013-04-07 06:31:33错误:[PDOException] SQLSTATE [42000]:语法错误或访问冲突: 1064您的SQL语法有错误;检查对应于您的MySQL服务器版本的手册,以获得在第1行register附近使用的正确语法
#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\\http://www.windows.com/windows/index.php?title=########################################################################################################################### \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','您已经注册并登录!
$ 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','您的帐户已创建,您很快就会收到一封电子邮件来验证您的帐户。验证后,您将可以登录。 '));
$ 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','无法创建您的帐户,请重试。'),'default',array('class'=>消息警告));
}
}
}


解决方案>

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

自动创建

若要发现是否未找到 User 模型,请尝试此操作;

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

// ....
}

输出 User 。如果它输出 AppModel Model ,那么CakePHP正在使用通用模型,表示找不到您的用户-model。




  • 清除 app / tmp / cache / models app / tmp / cache / persistent 目录,或者如果您使用APC,请清除用户缓存。

  • 您的控制器中有多个模型,请检查 User 模型是否在您的 $ uses 数组

  • 检查您的User模型是否在此文件中: app / Model / User.php ,类名为 code>(单数,不是复数



正确加载(调试示例显示 User ,而不是 AppModel ),你实际上有一个 register()方法,如果没有,你应该创建if。



希望这有助于。 / p>

[Update]



只是意识到您正在使用Users-plugin, >错误用户模型。使用插件时,您还需要使用插件中的 User 模型,而不是您自己的 User 模型。



如果您有自己的用户模型,您应该将其重命名为 User (例如 AppUser ),以防止CakePHP加载错误的模型。按照plugin-documentation中的说明,了解如何扩展插件组件:扩展模型



但是,如果要从插件中扩展 User 模型,还需要扩展插件的 UsersController ,请参阅扩展控制器



如果您不要需要扩展插件 - 模型和控制器,从您的应用程式(例如 app / Model / User.php app / Controller / UsersController.php



我保留了上面的原始建议,因为它们在其他情况下仍然有用


Iam using cakephp 2.2.1 and trying to use the cakedc users plugin, when I try to create a user I get the following error, can anyone give some tips or advice to resolve this error - I have loaded all tables using the migrations plugin. Thanks in advance :).

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}

Add function:

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'));
            }
        }
    }

解决方案

For some reason, your User model is not being used by CakePHP or the register() method does not exist. CakePHP will execute non-existing methods as SQL statements. If a Model is not being used, CakePHP will automatically create a model, based on your AppModel

To discover if the User model was not found, try this;

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

    // ....
}

This should output User. If it outputs AppModel or Model then CakePHP is using a generic model, which indicates that it couldn't find your User-model.

  • Clear your app/tmp/cache/models and app/tmp/cache/persistent directories, or if you're using APC, clear the user-cache.
  • if you're using more than one Model in your controller, Check that the User model is in your $uses array
  • Check that your User model is inside this file: app/Model/User.php and that the classname is User (singular, not plural)

If, on the other hand, your model is properly loaded (the debugging example shows User, not AppModel), then check if you actually have a register() method, if not, you should create if.

Hope this helps.

[Update]

Just realised you're using the Users-plugin, and may be loading the wrong User-model. When using the plugin, you also need to use the User model from the plugin, not your own User model.

If you do have your own User-model, you should rename it to something other than User (e.g. AppUser) to prevent CakePHP from loading the wrong Model. Follow the instructions in the plugin-documentation on how to extend the plugins 'components' here: extending the Model

However, if you want to extend the User model from the plugin, you'll also need to extend the UsersController of the plugin, see Extending the Controller

If you dont need to extend the plugin-Model and Controller, it's best to remove both from your application (e.g. app/Model/User.php and app/Controller/UsersController.php)

I've kept my original suggestions above, because they still may be helpful in other situations

这篇关于错误PDO异常SQLSTATE [42000]使用CakeDC Users插件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆