cakephp:从下拉列表中为报告添加过滤器

问题描述 投票:0回答:1

我有一个生成视图和excel文件的报告。我很想在两者中显示它的所有行。用户需要根据日期字段“end_date”过滤数据。在我看来,我把它说:

<?php
            $options = array();
            $options[0] = 'All';
            $options[1] = 'Due last 6 months';
            $options[2] = 'Due current month';
            $options[3] = 'Due next 3 months';
            $options[4] = 'Due next 6 months';
            $options[5] = 'Due next 12 months';            
        ?>

        <div class="row">
            <?= $this->Form->create() ?>
                <fieldset>                    
                    <div class="col-sm-2"><?= $this->Form->input('select_period', array('options' => $options)); ?></div>
                    <div class="col-sm-2" style="padding-top:25px;"><?= $this->Form->button('Search', ['type' => 'submit', 'class' => 'btn btn-primary']); ?></div>
                </fieldset>
            <?= $this->Form->end() ?>
        </div>

在我的控制器中,我使用此功能:

public function reportReplacement()
{
    $assetsAssignations = $this->AssetsAssignations->find()
        ->contain([
            'AssetStatuses',
            'Assets' => [
                'AssetTypes'
                ],
            'Clients' => [
                'ClientTypes'
                ],
        ])
        ->order([
                 'AssetTypes.name' => 'asc',
         ]);
    $filter = $this->Filter->prg($assetsAssignations);
    $_filename = "xls_report_replacement_" . date('Ymd');
    $this->set(compact('assetsAssignations', '_filename'));
}     

我试着将它放在我的控制器初始化中,但它不起作用:

if ($this->request->param('action') == 'reportReplacement') {
        $select_period = $this->request->query('select_period'); // I want to get the selected [0], [1], etc..  when I debug, I always get null.

我也没能找到我必须在我的查询中将选项作为条件的地方,例如:

如果选项[1],则'end_date'> =(TODAY-6个月)和'end_date'<= TODAY;

如果选项[2],则'end_date'= TODAY(月);

如果选项[1],则'end_date'> = TODAY AND'end_date'<=(TODAY + 3个月);

等等

有什么帮助吗?

cakephp cakephp-3.x
1个回答
0
投票

感谢Greg,我找到了解决方案:

在视图中:

        <?php
            $options = array();
            $options[0] = 'Due last 6 months';
            $options[1] = 'Due current month';
            $options[2] = 'Due next 3 months';
            $options[3] = 'Due next 6 months';
            $options[4] = 'Due next 12 months';            
        ?>

        <div class="row">
            <?= $this->Form->create() ?>
                <fieldset>                
                    <div class="row">
                        <div class="col-xs-3"><?= $this->Form->input('select_period_id', ['options' => $options, 'empty' => true, 'label' => __('Select Period')]) ?></div>
                        <div class="col-sm-2" style="padding-top:25px;"><?= $this->Form->button(__('Submit')) ?></div>
                    </div>
                </fieldset>
            <?= $this->Form->end() ?>

        </div>

在控制器中:

use Cake\I18n\Time;

并在功能:

    $data = $this->request->data;
    $select_period = $this->request->data('select_period_id');
    $today = Time::now()->format('Y-m-d');
    $second_date = Time::now();
    $assetsAssignations = $this->AssetsAssignations->find()
        ->contain(['Assets']);

    if($this->request->is(['patch', 'post', 'put'])) 
    {
        if ($select_period == 0) {                
            $second_date = $second_date->modify('-6 months');                
            $second_date = $second_date->format('Y-m-d');
            $assetsAssignations->where([
                'end_date >=' => $second_date,
                'end_date <=' => $today
                    ]);
        } elseif ($select_period == 1) {
            $second_date = $second_date->modify('1 month');
            $second_date = $second_date->format('Y-m-d');
            $assetsAssignations->where([
                'end_date >=' => $today,
                'end_date <=' => $second_date
                    ]);

        } elseif ($select_period == 2) {
            $second_date = $second_date->modify('3 months');
            $second_date = $second_date->format('Y-m-d');
            $assetsAssignations->where([
                'end_date >=' => $today,
                'end_date <=' => $second_date
                    ]);
        } elseif ($select_period == 3) {
            $second_date = $second_date->modify('6 months');
            $second_date = $second_date->format('Y-m-d');
            $assetsAssignations->where([
                'end_date >=' => $today,
                'end_date <=' => $second_date
                    ]);
        } elseif ($select_period == 4) {
            $second_date = $second_date->modify('12 months');
            $second_date = $second_date->format('Y-m-d');
            $assetsAssignations->where([
                'end_date >=' => $today,
                'end_date <=' => $second_date
                    ]);
        }            
    }

    $this->set(compact('assetsAssignations'));
} 
© www.soinside.com 2019 - 2024. All rights reserved.