I am making a request as follows.
http://localhost/sistema-agenda-server/tarefas/listAll?id=1
I asked to see the response in the generated sql and got the following answer:
SELECT * FROM tarefas WHERE idu_tar=:id
: id was not changed to 1.
public function get_listAll($id = null, $filtroData = null, $filtroDuracao = null, $ordem = null)
{
$sql = "SELECT * FROM tarefas WHERE idu_tar=:id ";
$vars = array(':id' => $id);
if(!is_null($filtroData))
{
$sql .= " AND datf_tar = :filtroData";
$vars[':filtroData'] = $filtroData;
}
if(!is_null($filtroDuracao))
{
$sql .= " AND tee_tar = :filtroDuracao";
$vars[':filtroDuracao'] = $filtroDuracao;
}
if(!is_null($ordem))
{
$sql .= " ORDER BY :ordem";
$vars[':ordem'] = $ordem;
}
else
{
$sql .= " ORDER BY gra_tar";
}
$stmt = DB::prepare($sql);
$stmt->execute($vars);
$tarefas = $stmt->fetchAll();
if($tarefas != null)
return $tarefas;
else
throw new Exception("Erro ao obter tarefas!");
}
If I change:
$vars = array(':id' => $id);
to $vars = array(':id' => $_GET['id'])
;
it works.
I believe it to be something simple. Can someone help me?
This is the route according to the controller, action, and parameter specification for my classes:
$app->get('/:controller/:action(/:parameter)',
function ($controller, $action, $parameter = null) use($app){
include_once "classes/{$controller}.php";
$classe = new $controller();
$retorno = call_user_func_array(array($classe, "get_" . $action), array($parameter));
echo '{"result":' . json_encode($retorno) . '}';
});