KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Undefined index after apply filter #3108

Closed GHSix opened this topic on on Jul 25, 2023 - 4 comments

GHSix commented on Jul 25, 2023

I call my query and filter it inside dashboard dataSource function for a specific graph widget. But if I filter it and the result is 0 rows, Koolreport will explod on an "Undefined index" situation and the entire panel will not work.

I'm thinking in a solution where I add a custom process after the filter so I can check and if there are no rows, I can add a dummy row.

But I can't figure out how to get it working or if this solution will ever work.

Here is my code to add some context:

protected function dataSource()
    {
        $filter = [['val_acima_lr', '>', '0']];
        $view = substr($this->name, -1);

        switch ($view) {
            case 'B':
            case 'C':
                $filter[] = ['ano', '=', substr($this->params['selectedRow'][0], 0, 4)];
                break;
        }

        return $this->dashboard()->getDataG('queryAvb')
            ->run()
            ->process(\koolreport\processes\Filter::process(
                $filter
            )
                ->pipe(new \koolreport\processes\Custom(function($row){

                    if (count($row) < 1) {
                        $row = [[
                            'dt' => '2000-01-01 00:00:00',
                            'ano' => '2000',
                            'val_avb' => 0,
                            'val_acima_lr' => 1
                        ]];
                    }

                    return $row;
                }))
            )
            ->process(\koolreport\processes\Group::process([
                'by' => $view == 'A'? 'ano' : 'dt',
                'sum' => ['val_avb', 'val_acima_lr']
            ]));
    }

    protected function fields()
    {
        return [
            DateTime::create('dt')
                ->label(Lang::t('Date'))
                ->displayFormat(Lang::t(substr($this->name, -1) == 'A'? 'Y' : 'Y-m')),
            Currency::create('val_acima_lr')
                ->label(Lang::t('Above'))
                ->symbol()
                ->decimals(2)
                ->BRL(),
            Currency::create('val_avb')
                ->label(Lang::t('Endorsed'))
                ->symbol()
                ->decimals(2)
                ->BRL(),
        ];
    }

This ->pipe with a Custom process is my attempt to create the dummy row, but it is not working.

Without this pipe in place, I will get the "Undefined index: dt" error. With the pipe, I will get an eternal loading animation.

What am I doing wrong?

GHSix commented on Jul 26, 2023

I guess I got it.

$r = $this->dashboard()->getDataG('queryAvb')
            ->run()...

if ($r->count() < 2) {
            $r = [[
                'dt' => NULL,
                'ano' => NULL,
                'val_avb' => 0,
                'val_acima_lr' => 0.001
            ]];
        }
return $r;
GHSix commented on Jul 26, 2023

I still understand it as a Koolreport Dashboard bug.

If there is no rows to plot, show a nice message inside the widget saying there is no data to show or a blank plot, but to show an error or an eternal loading is not kool.

And, the way to do that would be somewhere in Dashboard classes when it faces a empty dataSource, it could get the fileds will be needed and automatic create a dummy row with all NULL values. At least with D3, it plots a nice blank graph.

KoolReport commented on Jul 27, 2023

Thank you very much for letting us know. I have forwarded your suggestion to dev.team. Personally I think the same as you did, it would be better if null check and friendly message available. It would be a time-saving point for developer.

GHSix commented on Jul 27, 2023

Nice. Thank you!

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed

Dashboard