cakephp结果集用于多个连接和聚合函数...即。复杂的查询

时间:2014-09-24 12:56:00

标签: mysql arrays cakephp-2.1

我在我的一个项目中使用cakephp。我需要的是使用单个模型和单个数组来处理复杂的查询。因为我是cakephp的新手,所以我在这里非常糟糕:

$rs = $this->User->query("
                SELECT  (wd.wajebaat_amt) as commited,
                SUM(pd.sila_waje) as paid, 
                (wd.wajebaat_amt-sum(pd.sila_waje)) as balance, 
                FROM wajebaat_details as wd
                LEFT JOIN waje_pay_details as pd ON (pd.waje_id=wd.waje_id) 
                WHERE wd.hof_id="123" and wd.year="2010"
                GROUP BY wd.waje_id");

print_r($rs); exit();

//它将输出显示为

Array
(
    [0] => Array
        (
            [wd] => Array
                (
                    [commited] => 252000
                )

            [0] => Array
                (
                    [paid] => 253829
                    [balance] => -1829
                )

        )

)

//但我需要格式化

Array
(
    [0] => Array
        (
            [wd] => Array
                (
                    [commited] => 252000
                    [paid] => 200000
                    [balance] => 52000
                )

        )

)

1 个答案:

答案 0 :(得分:0)

您可以在cakephp 2.5中使用Hash(实用程序)方法format()将嵌套数组转换为字符串,在先前版本的cakephp中方法设置为(),

Hash::format(array $data, array $paths, $format)

示例:

$result = Hash::format($rs,array('{n}.wd.commited','{n}.wd.0.paid','{n}.wd.0.balance'),'%1$d,%2$d,%$d');

输出:

252000,2000000,52000

更多格式化选项参考cook book of cakephp