我尝试在Phalcon
控制器的操作中执行原始查询:
public function showAction()
{
$data=array();
// header("Access-Control-Allow-Origin: *");
$query = new Query(
'select a.id_jenis_bu,b.jenis_bu, sum(jumlah_bu) as jumlah from CakupanBu a
inner join JenisBu b on a.id_jenis_bu=b.id_jenis_bu
group by a.id_jenis_bu',
$this->getDI()
);
// Execute the query returning a result if any
$bus = $query->execute();
var_dump($bus);
foreach ($bus as $bu) {
$data[] = array(
'kode' => $bu->id_jenis_bu,
'jenis' => $bu->jenis_bu,
'jumlah' => $bu->jumlah,
);
}
var_dump($data);
return json_encode($data);
}
我很遗憾,这不起作用,我最终得到了一个错误。
有人能给我解决方案吗?
答案 0 :(得分:1)
即使我强烈建议你move all the logic for accessing data into a Model,所以为了利用MVC模式,从Phalcon控制器运行原始SQL查询的正确方法是:
$query =
'SELECT a.id_jenis_bu,b.jenis_bu, sum(jumlah_bu) as jumlah
FROM CakupanBu a
INNER JOIN JenisBu b
ON a.id_jenis_bu=b.id_jenis_bu
GROUP BY a.id_jenis_bu';
$this->db->query($query);