Joomla 查询显然运行了不止一次
我在 Joomla 网站的管理员中有一个报告组件。用户可以从可用报告类型的下拉列表中进行选择,然后调用函数来运行查询并输出 CSV 文件。在大多数情况下,这是有效的。然而,对于一份报告,我通过 Joomla 函数得到的结果数量与直接在 mySQL 中运行查询得到的结果数量不同。我添加了一个 error_log 来查看发生了什么,看起来好像查询运行了两次。 (也许吧。)
这是调用该函数的代码:
function getFullRedeemActivity($start, $end){
return getReportFullRedeemActivityByDate("v.UpdateDT", strtotime($start),strtotime($end));
}
传入 $start 和 $end;那里没有问题。问题出在 getReportFullRedeemActivityByDate 函数中。这是代码:
function getReportFullRedeemActivityByDate($start, $end, $limitStart=null, $limitRows=null){
//open db
$db =& JFactory::getDBO();
$where = ($low && $high) ? " and v.UpdateDT between ".$db->quote($low)." and ".$db->quote($high) : "";
$sort = "v.UpdateDT";
$limit = (is_int($limitStart) && is_int($limitRows)) ? " limit ".$db->quote($limitstart).", ".$db->quote($limitRows) : "";
//set query
$query = "select r.RedeemAmt,
v.VoucherNbr, v.BalanceInit, v.UpdateDT, v.BalanceCurrent,
m.SkuAbbr, m.MerchantNm,
a.name, a.email, a.company, a.address1, a.address2, a.city, a.state, a.zip, a.phone
from arrc_RedeemActivity r
left outer join arrc_Voucher v on v.VoucherID = r.VoucherID
left outer join arrc_Merchant m on m.MerchantID = r.MerchantID
left outer join jos_customers_addresses a on a.id = r.AcctID
{$where}
order by {$sort} {$limit}";
$db->setQuery($query);
if (!$db->query()) error_log($db->stderr());
if (!$db->getNumRows()){
JError::raiseWarning( 100, 'No records returned' );
return false;
}
else{
error_log("there are ". $db->getNumRows()." rows");
}
//loop out records into array
foreach ($db->loadAssocList() as $row){
$data[$row['BalanceCurrent']] = $row;
return $data;
}
}
当我查看 error_log 时,我看到的是:
[Tue Oct 19 09:37:30 2010] [error] [client xxx.xx.xx.xxx] there are 5 rows, referer: http://mysite.com/administrator/index.php?option=com_arrcard§ion=reports
[Tue Oct 19 09:37:30 2010] [error] [client xxx.xx.xx.xxx] there are 2 rows, referer: http://mysite.com/administrator/index.php?option=com_arrcard§ion=reports
就像那样;一个接一个。鉴于打印出来的行不在循环内,我无法弄清楚为什么它显然运行该部分两次。
有什么想法吗?
I have a reporting component in the administrator of a Joomla site. The user can choose from a dropdown list of available report types, which then calls a function to run the query and output a CSV file. For the most part this works. However, for one report, I'm getting a different number of results through the Joomla function than I do if I run the query directly in mySQL. I added an error_log to see what was happening, and it appears as if the query is running twice. (Maybe.)
Here's the code that calls the function:
function getFullRedeemActivity($start, $end){
return getReportFullRedeemActivityByDate("v.UpdateDT", strtotime($start),strtotime($end));
}
$start and $end are passed in; no problems there. The issue is in the getReportFullRedeemActivityByDate function. Here's the code for that:
function getReportFullRedeemActivityByDate($start, $end, $limitStart=null, $limitRows=null){
//open db
$db =& JFactory::getDBO();
$where = ($low && $high) ? " and v.UpdateDT between ".$db->quote($low)." and ".$db->quote($high) : "";
$sort = "v.UpdateDT";
$limit = (is_int($limitStart) && is_int($limitRows)) ? " limit ".$db->quote($limitstart).", ".$db->quote($limitRows) : "";
//set query
$query = "select r.RedeemAmt,
v.VoucherNbr, v.BalanceInit, v.UpdateDT, v.BalanceCurrent,
m.SkuAbbr, m.MerchantNm,
a.name, a.email, a.company, a.address1, a.address2, a.city, a.state, a.zip, a.phone
from arrc_RedeemActivity r
left outer join arrc_Voucher v on v.VoucherID = r.VoucherID
left outer join arrc_Merchant m on m.MerchantID = r.MerchantID
left outer join jos_customers_addresses a on a.id = r.AcctID
{$where}
order by {$sort} {$limit}";
$db->setQuery($query);
if (!$db->query()) error_log($db->stderr());
if (!$db->getNumRows()){
JError::raiseWarning( 100, 'No records returned' );
return false;
}
else{
error_log("there are ". $db->getNumRows()." rows");
}
//loop out records into array
foreach ($db->loadAssocList() as $row){
$data[$row['BalanceCurrent']] = $row;
return $data;
}
}
When I look at my error_log, what I see is:
[Tue Oct 19 09:37:30 2010] [error] [client xxx.xx.xx.xxx] there are 5 rows, referer: http://mysite.com/administrator/index.php?option=com_arrcard§ion=reports
[Tue Oct 19 09:37:30 2010] [error] [client xxx.xx.xx.xxx] there are 2 rows, referer: http://mysite.com/administrator/index.php?option=com_arrcard§ion=reports
Just like that; one after the other. Given that the line that prints that out is not inside a loop, I can't figure out why it's apparently running through that section twice.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,并非所有返回的记录在 BalanceCurrent 列中都有值,因此当 foreach 命中此行时:
它不会为这些记录创建行。我将字段名称切换为所有行肯定都有的 id 列之一,一切都很好。
It turns out that not all records being returned had a value in the BalanceCurrent column, so when the foreach hit this line:
it wasn't creating a row for those records. I switched the field name to one of the id columns that all rows definitely have, and everything's fine.