CakePHP搜索插件过滤后获取字段总数 [英] CakePHP Get Field Totals After Search Plugin Filtering

查看:192
本文介绍了CakePHP搜索插件过滤后获取字段总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用CakeDC搜索插件进行过滤后获取所有结果的字段总计。



在我的模型中,我有:

$ p $ public function getFieldAmountTotal($ fieldNames){
//不能使用递归-1,因为它包含了当前的过滤
//这样只能通过id
获取总数//不能因为过滤相关表
// $ totalAmounts = $ this-> find('all',array('fields'=> $ fieldNames));
$ totalAmounts = $ this-> find('all',array('fields'=> $ fieldNames,'group'=>'MovieStar.id'));
$ grandTotal = array();
foreach($ fieldNames as $ fieldName){
$ grandTotal [$ fieldName] = 0;

foreach($ totalAmounts as $ amount){
foreach($ fieldNames as $ fieldName){
$ grandTotal [$ fieldName] + = $ amount ['MovieStar'] [$ fieldName的];


debug('$ grandTotal');
debug($ grandTotal);
返回$ grandTotal;



$ b

当我使用CakePHP过滤器插件时,在会话中,并被自动传入。



如何使用当前过滤器插件表单设置过滤?

解决方案

我想出了一个办法。



型号:

  public function getFieldAmountTotal($ rowArray,$ fieldNames){
//不能使用递归-1,因为它包含了当前的过滤
//这只会通过id
/ /无法拉ID,因为在相关表上过滤
// $ totalAmounts = $ this-> find('all',array('fields'=> $ fieldNames));
$ totalAmounts = $ this-> find('all',array('fields'=> $ fieldNames,'group'=>'MovieStar.id'));
$ grandTotal = array();
foreach($ fieldNames as $ fieldName){
$ grandTotal [$ fieldName] = 0;
}
// foreach($ totalAmounts as $ amount){
foreach($ rowArray as $ thisRow){
foreach($ fieldNames as $ fieldName){
$ grandTotal [$ fieldName] + = $ thisRow ['MovieStar'] [$ fieldName];


debug('$ grandTotal');
debug($ grandTotal);
返回$ grandTotal;

$ / code>

控制器:

  $这 - > Prg-> commonProcess(); 
$ this-> paginate = array('conditions'=> $ this-> MovieStar-> parseCriteria($ this-> Prg-> parsedParams()),'limit'=> 100);
$ movieStars = $ this-> paginate();

//获取星级总金额
$ amountFields = array('amount','loss_axis','loss_mgr1','loss_mgr2','loss_rep');
$ reject_totals = $ this-> MovieStar-> getFieldAmountTotal($ movieStars,$ amountFields);

结果:

 < (浮点型)4074.97,
'loss_axis'=>(float)22,
'loss_mgr1'=>(float)code> array(
'amount'=> 0,
'loss_mgr2'=>(float)0,
'loss_rep'=>(float)0


I am trying to get the field totals for all results after filtering with the CakeDC search plugin.

In my model I have:

public function getFieldAmountTotal( $fieldNames){
    // Can't use recursive -1 because it includes current filtering
    // This will only grab the total by id
    // Can't not pull id because filtering on related tables
    //$totalAmounts = $this->find( 'all', array('fields' => $fieldNames));
    $totalAmounts = $this->find( 'all', array('fields' => $fieldNames, 'group' => 'MovieStar.id'));
    $grandTotal = array();
    foreach($fieldNames as $fieldName){
            $grandTotal[$fieldName] = 0;
    }
    foreach($totalAmounts as $amount){
        foreach($fieldNames as $fieldName){
                $grandTotal[$fieldName] += $amount['MovieStar'][$fieldName];
        }
    }
    debug('$grandTotal');
    debug($grandTotal);
    return $grandTotal;
}       

This worked great when I was using the CakePHP filter plugin because all filtering was stored in the session and was automatically passed in.

How would I filter in the find using the current filter plugin form settings?

解决方案

I figured out one way to do it. I pass the rows from the controller to the model.

Model:

public function getFieldAmountTotal($rowArray, $fieldNames){
        // Can't use recursive -1 because it includes current filtering
        // This will only grab the total by id
        // Can't not pull id because filtering on related tables
        //$totalAmounts = $this->find( 'all', array('fields' => $fieldNames));
        $totalAmounts = $this->find( 'all', array('fields' => $fieldNames, 'group' => 'MovieStar.id'));
        $grandTotal = array();
        foreach($fieldNames as $fieldName){
                $grandTotal[$fieldName] = 0;
        }
        //foreach($totalAmounts as $amount){
        foreach($rowArray as $thisRow){
            foreach($fieldNames as $fieldName){
                    $grandTotal[$fieldName] += $thisRow['MovieStar'][$fieldName];
            }
        }
        debug('$grandTotal');
        debug($grandTotal);
        return $grandTotal;
    }

Controller:

$this->Prg->commonProcess();
        $this->paginate = array( 'conditions' => $this->MovieStar->parseCriteria($this->Prg->parsedParams()), 'limit' => 100);
        $movieStars = $this->paginate();

        // Get total amounts for stars
        $amountFields = array('amount','loss_axis', 'loss_mgr1', 'loss_mgr2', 'loss_rep');
        $reject_totals = $this->MovieStar->getFieldAmountTotal($movieStars, $amountFields);

Result:

array(
    'amount' => (float) 4074.97,
    'loss_axis' => (float) 22,
    'loss_mgr1' => (float) 0,
    'loss_mgr2' => (float) 0,
    'loss_rep' => (float) 0
)

这篇关于CakePHP搜索插件过滤后获取字段总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆