douyun3022 2016-11-16 16:12
浏览 35
已采纳

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

I'm following this tutorial and i'm trying to create pagination with cakephp 2.9.2 and AngularJs1.5. The problem is that when i add this tutorial code, i'm keep getting error SQLSTATE[42000]: Syntax error or access violation.

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

{
    "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? What is the simplest way to achieve desired effect in my current cakephp version?

If you need any additional information's, please let me know and i will provide. Thank you in advance!

  • 写回答

1条回答 默认 最新

  • dongxue9997 2016-11-16 17:03
    关注

    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']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?