CakePHP查询参数OFFSET?

时间:2015-09-16 17:38:21

标签: php mysql cakephp

我尝试创建一个SELECT来返回一些值。我想要返回这些用LIMIT和OFFSET分页的值。为此,我尝试创建一个查询但不起作用并抛出mysql的语法异常。

我怎么能这样做?

public function findTarefas(){
            $this->autoRender = false;            
            $json = $this->request->input("json_decode", true);
            $id = $json["Pessoa"]["id"];
            $offset = $json["Pessoa"]["offset"];

            $tarefas = $this->Tarefa->query("SELECT * FROM responsavel_alunos RespAlunos "
                    . "INNER JOIN pessoas Responsavel ON (RespAlunos.pessoas_id = Responsavel.id) "
                    . "INNER JOIN pessoas Aluno ON (RespAlunos.pessoas_id1 = Aluno.id) "
                    . "INNER JOIN matriculas Matricula ON (Matricula.pessoas_id = Aluno.id) "
                    . "INNER JOIN turmas Turma ON (Matricula.turmas_id = Turma.id) "
                    . "INNER JOIN tarefas Tarefa ON (Tarefa.turmas_id = Turma.id) "
                    . "INNER JOIN disciplinas Disciplina ON (Tarefa.disciplinas_id = Disciplina.id) "
                    . "INNER JOIN pessoas Professor ON (Tarefa.pessoas_id = Professor.id) "
                    . "WHERE (Responsavel.id = ?) ORDER BY Tarefa.created DESC LIMIT 5 OFFSET ? ",
                    array($id, $offset)                    
                    );            

            $array;       
            if($tarefas){
                $array = array("status"=>"1", "result"=>$tarefas);
            }else{
                $array = array("status"=>"0", "result"=>$tarefas);
            }

            return json_encode($array);
        }

异常

{
    "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 ''0'' 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 ''0'' at line 1",
    "url": "/PainelEscolar/Tarefas/findTarefas.json",
    "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 ''0'' at line 1"
        ],
        "queryString": "SELECT * FROM responsavel_alunos RespAlunos INNER JOIN pessoas Responsavel ON (RespAlunos.pessoas_id = Responsavel.id) INNER JOIN pessoas Aluno ON (RespAlunos.pessoas_id1 = Aluno.id) INNER JOIN matriculas Matricula ON (Matricula.pessoas_id = Aluno.id) INNER JOIN turmas Turma ON (Matricula.turmas_id = Turma.id) INNER JOIN tarefas Tarefa ON (Tarefa.turmas_id = Turma.id) INNER JOIN disciplinas Disciplina ON (Tarefa.disciplinas_id = Disciplina.id) INNER JOIN pessoas Professor ON (Tarefa.pessoas_id = Professor.id) WHERE (Responsavel.id = ?) ORDER BY Tarefa.created DESC LIMIT 5 OFFSET ?"
    }
}

0 个答案:

没有答案