Error PDO Exception SQLSTATE[42000] when using Cak

2019-08-23 03:03发布

问题:

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

回答1:

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



回答2:

you should check the content in $this->request->data against the user table, maybe some value is not legal