CakePHP在搜索插件过滤后获取字段总计

时间:2014-03-07 20:18:05

标签: cakephp search plugins filter totals

在使用CakeDC搜索插件过滤后,我试图获取所有结果的字段总数。

在我的模特中,我有:

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;
}       

当我使用CakePHP过滤器插件时,这很有效,因为所有过滤都存储在会话中并自动传入。

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

1 个答案:

答案 0 :(得分:0)

我想出了一种方法。我将行从控制器传递给模型。

型号:

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;
    }

控制器:

$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);

结果:

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