我在cakephp 3中遇到了我的db函数(postgres)的问题。这真的很奇怪,因为函数在选择窗口pgAdmin中工作正常但在cakephp中我没有结果。 这是我的postgres功能:
CREATE OR REPLACE FUNCTION aqua.chart_def()
RETURNS text AS
$BODY$
DECLARE
c CURSOR FOR select '[''' || to_char(create_date, 'YYYY-MM-DD HH24:MI')||''','||air_temp||']' def from aqua.measurements
where create_date >= now() - interval '2' hour and create_date <= now();
result text := '';
BEGIN
FOR r IN c LOOP
result := result||', '||r.def;
END LOOP;
return ltrim(result, ', ');
--return '[''2016-06-02 13:39'',28], [''2016-06-02 13:42'',23], [''2016-06-02 13:49'',24], [''2016-06-02 13:52'',29]';
END;
$BODY$
LANGUAGE plpgsql;
ALTER FUNCTION aqua.chart_def()
OWNER TO postgres;
此函数返回结果:['2016-06-02 13:39',28], ['2016-06-02 13:42',23], ['2016-06-02 13:49',24], ['2016-06-02 13:52',29]
。
最好的部分是如果我评论一下:
return ltrim(result, ', ');
包含此函数复制结果的组件行:
return '[''2016-06-02 13:39'',28], [''2016-06-02 13:42'',23], [''2016-06-02 13:49'',24], [''2016-06-02 13:52'',29]';
如上所述正在使用cakephp。这是怎么回事?在sql窗口中结果相同。
这是我的控制器:
<?php
namespace App\Controller;
use Cake\Core\Configure;
use Cake\Network\Exception\NotFoundException;
use Cake\View\Exception\MissingTemplateException;
use Cake\Event\Event;
use Cake\Datasource\ConnectionManager;
class ChartsController extends AppController {
public $name = 'Charts';
public $uses = null;
public $helpers = array('Html');
function beforeFilter(Event $event)
{
$this->setChartDef();
}
public function index(){
$this->viewBuilder()->layout('default');
}
public function setChartDef()
{
$connection = ConnectionManager::get('default');
$results = $connection->execute('select t.text def from aqua.chart_def() t')->fetchAll('assoc');
$this->set('chart_def', $results[0]['def']);
}
}
并查看:
<!--[if lt IE 9]>
<?php echo $this->Html->script('excanvas.min.js');?>
<![endif]-->
<?php
echo $this->Html->script(['jQplot/jquery.min.js', 'jQplot/jquery.jqplot.min.js', 'jQueryUI/jquery-ui.min.js', 'jQueryUI/jquery-ui.js', 'jQplot/plugins/jqplot.dateAxisRenderer.min.js']);
echo $this->Html->css(['jQplot/jquery.jqplot.min.css', 'jQueryUI/jquery-ui.css']);
?>
<div><?php print_r($chart_def); ?></div>
<div id="chart_temp" style="height:500px; width:500px; margin:auto; "></div>
<script>
$(document).ready(function(){
var line1=[<?php print_r($chart_def); ?>];
var plot1 = $.jqplot('chart_temp', [line1], {
title:'Temperatura',
axes:{
xaxis:{
renderer:$.jqplot.DateAxisRenderer
}
},
series:[{lineWidth:4, markerOptions:{style:'square'}}]
});
});
</script>