Cakephp抛出语法错误或访问冲突错误

时间:2016-11-16 16:12:38

标签: php cakephp

我正在关注这个tutorial,我正试图用cakephp 2.9.2和AngularJs1.5创建分页。问题是当我添加这个教程代码时,我不断收到错误SQLSTATE [42000]:语法错误或访问冲突。

这是我的控制器     

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

模型

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

一旦我访问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"
    }
}

有人可以解释一下我做错了什么。 Meybe教程是用cakephp3编写的,我使用的是cakephp2.9.2?在我目前的cakephp版本中实现预期效果的最简单方法是什么?

如果您需要任何其他信息,请告诉我,我会提供。提前谢谢!

1 个答案:

答案 0 :(得分:1)

至少你没有正确使用那个别名属性,请改为尝试:

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