简体   繁体   English

Cakephp引发语法错误或访问冲突错误

[英]Cakephp throws Syntax error or access violation error

I'm following this tutorial and i'm trying to create pagination with cakephp 2.9.2 and AngularJs1.5. 我正在按照本教程进行操作,并尝试使用cakephp 2.9.2和AngularJs1.5创建分页。 The problem is that when i add this tutorial code, i'm keep getting error SQLSTATE[42000]: Syntax error or access violation. 问题是,当我添加此教程代码时,我不断收到错误SQLSTATE [42000]:语法错误或访问冲突。

This is my controller 这是我的控制器

/**
 * Todos Controller
 */
class TodosController extends AppController
{
    public $components = array('RequestHandler', 'Paginator');

    /**
     * Index
     */
    public function index()
    {
        $this->Paginator->settings = array(
            'limit' => 5,
            'order' => array(
                'todo.todo_id' => 'asc'
            )
        );
        if (empty($this->request->params['paging'][$this->Todo->alias()])) {
            $paging = false;
        } else {
            $paging = $this->request->params['paging'][$this->Todo->alias()];
        }
        $this->set('Todos', $this->Paginator->paginate('Todo'));
        $this->set('paging', $paging);
        $this->set('_serialize', ['Todos', 'paging']);
    }

Model 模型

   class Todo extends AppModel {

        public $primaryKey = 'todo_id';

        public $validate = array(
            'title' => array(
                'notBlank' => array(
                    'rule' => array('notBlank'),
                ),
            ),
            'user' => array(
                'notBlank' => array(
                    'rule' => array('notBlank'),
                ),
            ),
            'description' => array(
                'notBlank' => array(
                    'rule' => array('notBlank'),
                ),
            ),
        );
    }

And once i visit myapp.com/rest/todos this is error i'm geting 而且一旦我访问myapp.com/rest/todos,这就是我得到的错误

{
    "code": 500,
    "name": "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 'alias' at line 1",
    "message": "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 'alias' at line 1",
    "url": "\/rest\/todos",
    "error": {
        "errorInfo": [
            "42000",
            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 'alias' at line 1"
        ],
        "queryString": "alias"
    }
}

Can some one please explain me what i'm doing wrong. 有人可以解释一下我在做什么错。 Meybe tutorial is written in cakephp3 and i'm using cakephp2.9.2? Meybe教程是用cakephp3编写的,我正在使用cakephp2.9.2? What is the simplest way to achieve desired effect in my current cakephp version? 在当前的cakephp版本中,达到预期效果的最简单方法是什么?

If you need any additional information's, please let me know and i will provide. 如果您需要任何其他信息,请告诉我,我会提供。 Thank you in advance! 先感谢您!

At least you are not using that alias property correctly, Try this instead: 至少您没有正确使用该别名属性,请尝试以下操作:

public function index()
{
    $this->Paginator->settings = array(
        'limit' => 5,
        'order' => array(
            'todo.todo_id' => 'asc'
        )
    );

    $this->set('Todos', $this->Paginator->paginate('Todo'));

    if (isset($this->request->params['paging']) && !empty($this->request->params['paging'])) {
        $paging = $this->request->params['paging']['Todo']; // or simply $paging = $this->request->params['paging']; // depending upon your logic
    } else {
        $paging = false;
    }

    $this->set('paging', $paging);
    $this->set('_serialize', ['Todos', 'paging']);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM