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

8yoxcaq7  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(104)

我试图得到所有结果的字段总数过滤后,与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过滤器插件时,这非常有效,因为所有的过滤都存储在会话中,并且自动传递。
如何使用当前过滤器插件表单设置在查找中进行过滤?

kuhbmx9i

kuhbmx9i1#

我找到了一种方法,将行从控制器传递给模型。
产品型号:

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
)

相关问题