cakephp:将过滤器添加到下拉列表的报告中

时间:2018-06-21 15:13:23

标签: cakephp cakephp-3.x

我有一个报告可以生成为视图和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(month);

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

有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

多亏了格雷格,我找到了解决方法:

在视图中:

        <?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'));
}