如何根据cakephp中的其他列值求值

时间:2016-10-06 04:52:02

标签: cakephp sum

我是cakephp学习者并从事一个项目。 我有个问题。我做谷歌但无法找到正确的解决方案。我希望,我可以在这里找到解决方案。

这是我的代码。

function getCompanySales(){
  $model=ClassRegistry::init('Customer');
  $modelcompany=ClassRegistry::init('Company');
  $from = date("Y-m")."-01 00:00:00";
  $days_in_month = cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'));
  $to =  date("Y-m")."-".$days_in_month." 24:00:00";
  $companyname=$modelcompany->find('all');
  $companiessales=$model->find('all',array(
    'conditions' => array(
                    "AND" =>array(
                       "Customer.created >=" => $from,
                       "Customer.created <=" => $to
                                  )
                      ),
    'recursive'=>-1
      )
  );
 return $companiessales;
} // Get Company list

我得到了结果

    Array ( 
    [0] => Array ( [Customer] => Array ( [gateway] => 10 [amount] => 349 ) ) 
    [1] => Array ( [Customer] => Array ( [gateway] => 7 [amount] => 150 ) ) 
    [2] => Array ( [Customer] => Array ( [gateway] => 13 [amount] => 349 ) ) 
    [3] => Array ( [Customer] => Array ( [gateway] => 10 [amount] => 350 ) ) 
    [4] => Array ( [Customer] => Array ( [gateway] => 7 [amount] => 100 ) ) 
    [5] => Array ( [Customer] => Array ( [gateway] => 9 [amount] => 299 ) ) 
    [6] => Array ( [Customer] => Array ( [gateway] => 7 [amount] => 249 ) ) 
    [7] => Array ( [Customer] => Array ( [gateway] => 10 [amount] => 249 ) ) 
    [8] => Array ( [Customer] => Array ( [gateway] => 7 [amount] => 299 ) ) 
    [9] => Array ( [Customer] => Array ( [gateway] => 12 [amount] => 199 ) ) 
    [10] => Array ( [Customer] => Array ( [gateway] => 7 [amount] => 150 ) )      )

但我想用相同的网关来比较值

    array(
    [7] => 948 //sum of gateway 7
    [9] => 299 //sum of gateway 9
    [10] => 948 //sum of gateway 10
    [12] => 199 //sum of gateway 12
    [13] => 349 //sum of gateway 13
    )

在另一个查询中=&gt;

    array(
    [7] => company 1
    [9] => company 2 
    [10] => company 3
    [12] => company 4
    [13] => company 5

    )

我希望得到最终结果

    array(
    [company1] => 948
    [company1] => 299
    [company1] => 948
    [company1] => 199
    [company1] => 349
    )

感谢您的帮助。我确定我会轻松解决问题。

1 个答案:

答案 0 :(得分:1)

感谢stackoverflow给我提出想法并找到解决方案。 我首先通过以下代码将网关数量和组合在一起找到客户列表。 在视图中使用自定义帮助程序按ID获取名称。那是什么,我一直在寻找。

    $companiessales=$model->find('all',array(
      'fields'=>array(
      'SUM(amount) AS amount',
      'gateway'
      ),
   'group' => 'Customer.gateway',
   'conditions' => array(
                "AND" =>array(
                    "Customer.created >=" => $from,
                    "Customer.created <=" => $to
                    )
                ),
   'recursive'=>-1
   )
  );

以下是通过id获取网关名称的代码(我们通过对它们进行分组来制作主要ID)。

    function getGatewayName($id){
     $model=ClassRegistry::init('Gateway');
     $companiess=$model->find('first',array(
         'fields'=>array('fullname'),
         'conditions'=>array(
         'Gateway.id' => $id,
         ),   
        'recursive'=>-1
       )
      );
      $name=$companiess['Gateway']['fullname'];
      return $name;
    } // Get Payment Gateway Name