doutuoshou8915
2014-09-25 15:20 阅读 26
已采纳

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 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 :)

    点赞 评论 复制链接分享

相关推荐