2014-09-25 111 views
0

我已经构建了一种机制来将查询结果格式化为具有特定模式的数组。 例如:PHP - 使用'阵列样式'格式化查询结果

<?php 

// Pattern 
$aPattern = array(
    'cust_id' => array(
     '@detail' => array(
      '@name' => '%{forename} {name}', 
      '@company' => 'company' 
     ), 
     '@orders_total' => 
      array('article' => 'qty') 
    ) 
); 

// Query 
$sQuery = "SELECT `cust`.*, `order`.`article`, SUM(`quantity`) AS qty 
      FROM `cust` LEFT JOIN `order` ON `cust`.`id` = `order`.`cust_id` 
      GROUP BY `cust`.`id`, `order`.`article`"; 

// Run query and set fetch mode 
$oStatement = Data::query($sQuery)->setFetchPattern($aPattern); 

// Fetch results 
$aRes = $oStatement->fetchAll(); 

它工作正常,但格式化大量的行(> 5000),当它需要太多的时间。 我一直在寻找可以更有效地完成工作而没有成功的现有解决方案。 在发布代码以询问是否有人可以帮助我改进它之前,请确定是否有人知道是否有PHP类或者类似已经执行此类工作的类?

在此先感谢,对不起任何StackOverflow误用

+0

你可能会更好地切换到ORP,如Propel,或Doctrine或Eloquent – 2014-09-25 15:25:25

回答

0

最后我发现了这个问题。我为结果集中找到的每一行运行fetch函数,并且每次将结果合并到全局结果数组时,显然合并步骤花费的时间太长(resursive函数)。我只是修改了获取函数,以便在递归函数中完成全局结果数组而不需要合并。现在它的工作速度很快:)