简体   繁体   English

CakePhp 2.x在Ajax请求上打印SQL查询

[英]CakePhp 2.x print sql query On Ajax Request

I am trying to print the mysql query on Ajax Request but it is not showing . 我正在尝试在Ajax请求上打印mysql查询,但未显示。

My View Page 我的查看页面

在此处输入图片说明

Clicking the Search button will call one Ajax Function 单击搜索按钮将调用一个Ajax函数

Ajax.js Ajax.js

$(document).ready(function(){

function random_number()
{
    return Math.floor(Math.random() * 255);
}
$("#btningSrch").click(function(){
    $("#rpts").html("<img src='https://xx.xx.xx.xx/project/img/ajaxLoader1.gif' />");
    alert("hi");
    $.ajax({
           type: "GET",
           url: "https://xx.xx.xx.xx/project/controller/view/"+random_number(),
           data: "startDate="+$("#startDate").val()+"&type="+$("#type").val()+"&orderby="+$("#orderby").val(),
           dataType: "html",
           success: function(msg){      
                alert(msg);
                $("#rpts").html(msg);
           }
         });
    return false;
  });
});

Controller.php Controller.php

function show_in_summary()
{
    $this->isLoggedIn();
    //pr($this->request); exit;
    //echo $this->request->is('ajax');
    if ($this->request->is('ajax'))
    {
        //echo 2222; exit; //$this->layout = 'index3';
        //pr($this->InTrafficSummary->txtChk($this->params['url']['startDate']));
        //die();
        //pr($this->request->query);exit;
        //if ($this->InTrafficSummary->txtChk($this->request->query['startDate']))
        //{
            $this->set('record',1);
            $this->loadModel('InOutTrafficSummary');
            $data = $this->InOutTrafficSummary->readInBoundTrafficSummary($this->request->query['startDate'],
            $this->request->query['type'],$this->request->query['orderby']);
            $this->set('reports',$data[0]);
            $this->set('total',$data[1]);
            $this->set('date',date("Y-m-d",strtotime($this->request->query['startDate'])));
            $this->set('type',$this->request->query['type']);
        //}
        //else
        //{
            //$this->set('reports',array());
            //$this->set('record',0);   
            //$this->set('total',array());
            //$this->set('date',date("Y-m-d",strtotime($this->params['url']['startDate'])));
            //$this->set('type',$this->params['url']['type']);
        //}
    }
}               

Model.php 模型.php

$rs = $this->find('all', array('joins' => array(array(
    'table' => 'InOutTrafficSummary',
    'alias' => 'Carriers',
    'type' => 'INNER',
    'conditions' => array(
     'Carriers.CarrierId = '.$useTable.'.FK_IngressCarrierId'))),
    'conditions' => array('InDate' => date("Y-m-d",strtotime("-1 days"))),
    'fields' => array(
'FK_IngressCarrierId,CarrierName,Sum(TotalCalls),Sum(ConnectedCalls),sum(Duration),(Sum(ConnectedCalls)/Sum(TotalCalls))*100,sum(Duration)/Sum(ConnectedCalls),sum(SaleAmount)'),'group' => 'FK_IngressCarrierId','order' => 'sum(Duration) DESC'));

echo $this->element('sql_dump'); exit;

I want to print the query above as it is not showing any records , i am assuming there is a mistake in the formation so need to view the entire query.All the other things are working fine. 我想在上面打印查询,因为它没有显示任何记录,我假设格式有误,因此需要查看整个查询。所有其他东西都工作正常。

I am a newbie to CakePHP. 我是CakePHP的新手。

尝试这个

debug($this->YOUR_MODEL_NAME->getDataSource()->getLog(false, false));

I added following helper function in my AppModel.php 我在AppModel.php中添加了以下辅助函数

/**
 * Get the last queries
 * Wrapper function
 */
public function getLastQueries() {
    return $this->getDataSource()->getLog(false, false);
}

You can easily then call it in your show_in_summary() function: 您可以轻松地在show_in_summary()函数中调用它:

debug($this->InOutTrafficSummary->getLastQueries());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM