doutuoshou8915 2014-09-25 15:20
浏览 62
已采纳

PHP - 使用'array like'模式格式化查询结果

I've build a mechanism to format query results as an array with a specific pattern. For example :

<?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();

It works fine, but when formatting lots of rows (> 5000) it takes too much time. I've been looking for existing solutions that could do the job more efficiently without success. Before posting code to ask if anyone can help me to improve it, do someone know if there is a PHP class or something like that which already performs this kind of work ?

Thanks in advance, and sorry for any StackOverflow misuse

  • 写回答

1条回答 默认 最新

  • duanbodai5166 2014-10-06 00:11
    关注

    Finally I found the problem. I was running the fetch function for each row found in the resultset and each time I merged the result to the global result array, obviously the merge step was taking too much time (resursive function). I just modified the fetch function so that global result array is just completed in the recursive function without needing to merge. Now it's working fast :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料