Valor_
Valor_

Reputation: 3591

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. 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!

Upvotes: 0

Views: 774

Answers (1)

Manohar Khadka
Manohar Khadka

Reputation: 2195

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

Upvotes: 1

Related Questions