代码之家  ›  专栏  ›  技术社区  ›  harryg

Cake PHP联接查询作为关联数组返回

  •  2
  • harryg  · 技术社区  · 11 年前

    好的,这是我的表/模型结构:模型是适当关联的。

    table structure

    我正在尝试查询 持有价值总和 对于给定的客户端id,将给定值和的日期作为数组键。

    我查阅了文档,为我在客户端模型中的查询生成了以下参数:

    $findParameters = array(
        'conditions' => array(
            'Account.client_id' => $clientId,
            'Holding.holding_date = LAST_DAY(Holding.holding_date)', //gets month-end dates only
            'MONTH(Holding.holding_date)' => array(3,6,9,12) //Use for quarter-end dates
            ),
        'fields' => array(
            'Holding.holding_date',
            'SUM(Holding.value) AS portfolio_value'
            ),
        'group' => array('Holding.holding_date')
    );
    

    当我通过执行

    $holdings = $this->Account->Holding->find( 'all', $findParameters );
    

    我得到这样的结果:

    Array
    (
        [0] => Array
            (
                [Holding] => Array
                    (
                        [holding_date] => 2009-12-31
                    )
    
                [0] => Array
                    (
                        [portfolio_value] => 273239.07
                    )
    
            )
    
        [1] => Array
            (
                [Holding] => Array
                    (
                        [holding_date] => 2010-03-31
                    )
    
                [0] => Array
                    (
                        [portfolio_value] => 276625.28
                    )
    
            )
    ...
    

    这很好,但我希望得到这样一个数组的结果:

    Array (
        [2009-12-31] => 273239.07
        [2010-03-31] => 276625.28
        ...
    )
    

    所以我试着做:

    $holdings = $this->Account->Holding->find( 'list', $findParameters )
    

    但我得到了一个错误:

    Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Account.client_id' in 'where clause'
    

    查询看起来好像不再对表执行联接。知道为什么吗,如果我用的话效果很好 all 而不是 list ? 如何获得我想要的结果?


    编辑:我已经用Cake的取得了我的成绩 hash 类,但想知道直接查询是否是一种更优越、更有效的方法。

    我的方法:

        $holdings = $this->Account->Holding->find( 'all', $findParameters );
    
        $values = Hash::extract($result, '{n}.0.portfolio_value');
        $dates = Hash::extract($result, '{n}.Holding.holding_date');
    
        $result = array_combine($dates, $values);
        return $result;
    
    2 回复  |  直到 11 年前
        1
  •  2
  •   Jérémie Parker    11 年前

    一般性意见

    CakePHP的ORM非常适合轻松查找查询(包括 group by sort …)。当涉及到更高级的东西时,您需要处理在PHP中检索到的数据,或者制作 plain SQL Query .

    关于关联阵列

    Model::find('all') Model::find('first') 将始终返回关联数组。其他find方法将返回不同类型的数组,但您将无法使用开箱即用的Model函数获得所需的结果。

    关于未添加到正确子阵列的字段

    当您有 SUM() , AVG() COUNT() 你可以在这里阅读 about virtual fields in SQL Queries

    如何在CakePHP中提取/格式化数组

    最简单的方法是使用 Hash 类,以提取数据并按所需方式格式化数据。

    使用 Hash::combine 你可以这样实现你想要的目标:

    $result = Hash::combine($holdings, '{n}.Holding.holding_date', '{n}.0.portfolio_value');
    
        2
  •  0
  •   zerokavn    11 年前

    请尝试

    $findParameters = array(
        'conditions' => array(
            'Account.client_id' => $clientId,
            'Holding.holding_date = LAST_DAY(Holding.holding_date)', //gets month-end dates only
            'MONTH(Holding.holding_date)' => array(3,6,9,12) //Use for quarter-end dates
            ),
        'fields' => array(
            'Holding.holding_date',
            'SUM(Holding.value) AS portfolio_value'
            ),
        'group' => array('Holding.holding_date'),
        'contain' => array('Account),
    );