kartik-v / yii2-dynagrid

Turbo charge the Yii 2 GridView with personalized columns, page size, and themes.
http://demos.krajee.com/dynagrid
Other
74 stars 66 forks source link

array_sum(): Addition is not supported on type string when using showPageSummary #245

Closed newadventure079 closed 6 months ago

newadventure079 commented 9 months ago

Prerequisites

Steps to reproduce the issue

  1. Create a dynagrid widget and set 'showPageSummary' => true

Expected behavior and actual behavior

PHP Warning – yii\base\ErrorException
array_sum(): Addition is not supported on type string

This line seems to be the issue: kartik-v/yii2-grid/src/ColumnTrait.php at line 380– array_sum(['67.57%', '78.73%', '75.34%', '78.63%', ...]) It looks like it's summing the string values for the percentages

I was expecting...

No errors

Logs

2024-02-07 14:45:02 [127.0.0.1][-][-][trace][yii\base\Application::bootstrap] Bootstrap with yii\bootstrap5\i18n\TranslationBootstrap::bootstrap()
2024-02-07 14:45:02 [127.0.0.1][-][-][trace][yii\base\Application::bootstrap] Bootstrap with yii\log\Dispatcher
2024-02-07 14:45:02 [127.0.0.1][-][-][trace][yii\web\UrlManager::parseRequest] [
    'rule' => '<controller:\\w+(-)*\\w+>/<id:\\d+>',
    'match' => false,
    'parent' => null,
]
2024-02-07 14:45:02 [127.0.0.1][-][-][trace][yii\web\UrlManager::parseRequest] [
    'rule' => '<controller:\\w+(-)*\\w+>/<action:\\w+(-)*\\w+>/<id:\\d+>',
    'match' => false,
    'parent' => null,
]
2024-02-07 14:45:02 [127.0.0.1][-][-][trace][yii\web\UrlManager::parseRequest] [
    'rule' => '<controller:\\w+(-)*\\w+>/<action:\\w+(-)*\\w+>',
    'match' => false,
    'parent' => null,
]
2024-02-07 14:45:02 [127.0.0.1][-][-][trace][yii\web\UrlManager::parseRequest] No matching URL rules. Using default URL parsing logic.
2024-02-07 14:45:02 [127.0.0.1][-][-][trace][yii\web\Application::handleRequest] Route requested: 'report/view-sell-through-report-results'
2024-02-07 14:45:02 [127.0.0.1][-][-][trace][yii\base\Controller::runAction] Route to run: report/view-sell-through-report-results
2024-02-07 14:45:02 [127.0.0.1][-][-][trace][yii\base\InlineAction::runWithParams] Running action: app\controllers\ReportController::actionViewSellThroughReportResults()
2024-02-07 14:45:02 [127.0.0.1][-][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Connection::open] Opening DB connection: mysql:host=project.cx2wvqp5phoa.us-east-1.rds.amazonaws.com;dbname=project;port=3306
    in /Users/me/Documents/code/htdocs/project/controllers/ReportController.php:134
2024-02-07 14:45:02 [127.0.0.1][-][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::query] SELECT `data` FROM `session` WHERE `expire`>1707342302 AND `id`='77d4dmi3lsedco98lc3q3ea98u'
    in /Users/me/Documents/code/htdocs/project/controllers/ReportController.php:134
2024-02-07 14:45:02 [127.0.0.1][-][77d4dmi3lsedco98lc3q3ea98u][info][yii\web\Session::open] Session started
    in /Users/me/Documents/code/htdocs/project/controllers/ReportController.php:134
2024-02-07 14:45:02 [127.0.0.1][-][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::query] SELECT * FROM `user` WHERE `user_id`=20961
    in /Users/me/Documents/code/htdocs/project/models/User.php:83
    in /Users/me/Documents/code/htdocs/project/controllers/ReportController.php:134
2024-02-07 14:45:02 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::query] SELECT * FROM `sell_through_report` WHERE (`event_id`=60806) AND (`dc_store_order_start_date`='2023-12-01') AND (`dc_store_order_end_date`='2023-12-31') AND (`store`='')
    in /Users/me/Documents/code/htdocs/project/controllers/ReportController.php:156
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::query] SELECT * FROM `event` WHERE `event_id`=60806
    in /Users/me/Documents/code/htdocs/project/controllers/ReportController.php:161
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][trace][yii\base\Module::getModule] Loading module: gridview
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-krajee-base/src/Config.php:313
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridViewTrait.php:1255
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridViewTrait.php:824
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][trace][yii\base\View::renderFile] Rendering view file: /Users/me/Documents/code/htdocs/project/views/report/sell-through-report-results.php
    in /Users/me/Documents/code/htdocs/project/controllers/ReportController.php:166
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][trace][yii\base\Module::getModule] Loading module: dynagrid
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-krajee-base/src/Config.php:313
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGrid.php:545
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGrid.php:582
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::query] SELECT `data` FROM `tbl_dynagrid` WHERE `id`='sell-through-report-grid_20961'
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGridStore.php:485
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGridStore.php:214
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGrid.php:815
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::query] SELECT * FROM `sell_through_report` WHERE (`event_id`='60806') AND (`dc_store_order_start_date`='2023-12-01') AND (`dc_store_order_end_date`='2023-12-31') AND (`store`='')
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/ColumnTrait.php:540
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/DataColumn.php:234
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridView.php:101
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::query] SELECT `id`, `name` FROM `tbl_dynagrid_dtl` WHERE (`dynagrid_id`='sell-through-report-grid_20961') AND (`category`='filter')
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGridStore.php:420
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGrid.php:1288
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridViewTrait.php:1716
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::query] SELECT `id`, `name` FROM `tbl_dynagrid_dtl` WHERE (`dynagrid_id`='sell-through-report-grid_20961') AND (`category`='sort')
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGridStore.php:420
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGrid.php:1294
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridViewTrait.php:1716
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][trace][yii\base\View::renderFile] Rendering view file: /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/views/config.php
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGrid.php:1298
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridViewTrait.php:1716
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridViewTrait.php:1729
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][trace][yii\base\View::renderFile] Rendering view file: /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/views/settings.php
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGridDetail.php:179
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGrid.php:1339
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridViewTrait.php:1716
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::query] SELECT `id`, `name` FROM `tbl_dynagrid_dtl` WHERE (`dynagrid_id`='sell-through-report-grid_20961') AND (`category`='filter')
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGridStore.php:420
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/models/DynaGridSettings.php:205
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/views/settings.php:27
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::query] SELECT `data` FROM `tbl_dynagrid_dtl` WHERE `id`='sell-through-report-grid_filter_00000000_20961'
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGridStore.php:485
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGridStore.php:214
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/models/DynaGridSettings.php:216
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][trace][yii\base\View::renderFile] Rendering view file: /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/views/settings.php
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGridDetail.php:179
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGrid.php:1366
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridViewTrait.php:1716
2024-02-07 14:45:03 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::query] SELECT `id`, `name` FROM `tbl_dynagrid_dtl` WHERE (`dynagrid_id`='sell-through-report-grid_20961') AND (`category`='sort')
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGridStore.php:420
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/models/DynaGridSettings.php:205
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/views/settings.php:27
2024-02-07 14:45:04 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::query] SELECT `data` FROM `tbl_dynagrid_dtl` WHERE `id`='sell-through-report-grid_sort_00000000_20961'
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGridStore.php:485
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGridStore.php:214
    in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/models/DynaGridSettings.php:216
2024-02-07 14:45:04 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][error][yii\base\ErrorException:2] yii\base\ErrorException: array_sum(): Addition is not supported on type string in /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/ColumnTrait.php:380
Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleError(2, 'array_sum(): Ad...', '/Users/me/Do...', 380)
#1 /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/ColumnTrait.php(380): array_sum(Array)
#2 /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/ColumnTrait.php(344): kartik\grid\DataColumn->calculateSummary()
#3 /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/ColumnTrait.php(328): kartik\grid\DataColumn->getPageSummaryCellContent()
#4 /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/ColumnTrait.php(232): kartik\grid\DataColumn->renderPageSummaryCellContent()
#5 /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridViewTrait.php(1074): kartik\grid\DataColumn->renderPageSummaryCell()
#6 /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridViewTrait.php(1042): kartik\grid\GridView->getPageSummaryRow()
#7 /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridView.php(127): kartik\grid\GridView->renderPageSummary()
#8 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/grid/GridView.php(358): kartik\grid\GridView->renderTableBody()
#9 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/widgets/BaseListView.php(160): yii\grid\GridView->renderItems()
#10 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/grid/GridView.php(326): yii\widgets\BaseListView->renderSection('{items}')
#11 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/widgets/BaseListView.php(135): yii\grid\GridView->renderSection('{items}')
#12 [internal function]: yii\widgets\BaseListView->yii\widgets\{closure}(Array)
#13 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/widgets/BaseListView.php(134): preg_replace_callback('/{\\w+}/', Object(Closure), '<div class="car...')
#14 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/grid/GridView.php(301): yii\widgets\BaseListView->run()
#15 /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-grid/src/GridView.php(112): yii\grid\GridView->run()
#16 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/base/Widget.php(146): kartik\grid\GridView->run()
#17 /Users/me/Documents/code/htdocs/project/vendor/kartik-v/yii2-dynagrid/src/DynaGrid.php(526): yii\base\Widget::widget(Array)
#18 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/base/Widget.php(146): kartik\dynagrid\DynaGrid->run()
#19 /Users/me/Documents/code/htdocs/project/views/report/sell-through-report-results.php(57): yii\base\Widget::widget(Array)
#20 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/base/View.php(347): require('/Users/me/Do...')
#21 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/base/View.php(257): yii\base\View->renderPhpFile('/Users/me/Do...', Array)
#22 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/base/View.php(156): yii\base\View->renderFile('/Users/me/Do...', Array, Object(app\controllers\ReportController))
#23 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/base/Controller.php(407): yii\base\View->render('sell-through-re...', Array, Object(app\controllers\ReportController))
#24 /Users/me/Documents/code/htdocs/project/controllers/ReportController.php(166): yii\base\Controller->render('sell-through-re...', Array)
#25 [internal function]: app\controllers\ReportController->actionViewSellThroughReportResults(60806, '2023-12-01', '2023-12-31', '')
#26 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#27 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#28 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('view-sell-throu...', Array)
#29 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('report/view-sel...', Array)
#30 /Users/me/Documents/code/htdocs/project/vendor/yiisoft/yii2/base/Application.php(384): yii\web\Application->handleRequest(Object(yii\web\Request))
#31 /Users/me/Documents/code/htdocs/project/web/index.php(24): yii\base\Application->run()
#32 {main}
2024-02-07 14:45:04 [127.0.0.1][20961][77d4dmi3lsedco98lc3q3ea98u][info][yii\db\Command::execute] INSERT INTO `session` (`data`, `id`, `expire`) VALUES ('__flash|a:0:{}__id|i:20961;__authKey|N;', '77d4dmi3lsedco98lc3q3ea98u', 1707428704) ON DUPLICATE KEY UPDATE `data`=VALUES(`data`), `expire`=VALUES(`expire`)

Code

echo DynaGrid::widget(
    [
    'storage'=>DynaGrid::TYPE_DB,
    'columns' => $columns,
    'theme'=>'panel-primary',
    'showPersonalize'=>true,
    'showSort'=>true,
    'showFilter'=>true,
    'allowFilterSetting'=>true,
    'allowSortSetting'=>true,
    'gridOptions'=>[
        'pjaxSettings' => [
            'loadingCssClass' => false,
        ],
        'floatHeader'=>true,
        'floatHeaderOptions' => ['position' => 'absolute'],
        'showPageSummary' => true, <---------problem line
        'pageSummaryPosition' => GridView::POS_TOP,
        'pageSummaryRowOptions' => ['class' => 'kv-page-summary'],
        'condensed'=>true,
        'striped'=>true,
        'pjax'=>true,
        'dataProvider'=>$dataProvider,
        'filterModel'=>$searchModel,
        'panel'=>[
            'heading'=>Icon::show('list-alt').' Sell Through Report',
            'before' => $extra_info,
            'after' => "{pager}",
        ],
        'toolbar' =>  [
            ['content'=>'{pager}'.$exportMenu.Html::a(Icon::show('redo'), Url::to(['/report/get-sell-through-report-results', 'reset' => 'true', 'event_id' => $event_model->event_id, 'dc_store_order_start_date' => $model->dc_store_order_start_date, 'dc_store_order_end_date' => $model->dc_store_order_end_date, 'store' => $model->store]), ['data-pjax'=>0, 'class' => 'btn btn-outline-secondary', 'title'=>'Reset Grid']).'{dynagridFilter}{dynagridSort}{dynagrid}'],
        ]
    ],
    'options'=>['id'=>'sell-through-report-grid','style' => 'overflow:hidden;'] // a unique identifier is important
    ]
);

Environment

Browsers

Operating System

Libraries

Isolating the problem

newadventure079 commented 9 months ago

New in PHP 8.3:

8.3.0   Now emits E_WARNING when array values cannot be converted to int or float. Previously arrays and objects where ignored whilst every other value was cast to int. Moreover, objects that define a numeric cast (e.g. GMP) are now cast instead of ignored.

https://www.php.net/manual/en/function.array-sum.php

tjura commented 6 months ago

Same here, you can work around this by adding pageSummaryFunc in your column setting

kartik-v commented 6 months ago

Yes this is expected - the default page summary action is to DO A SUMMATION. Your columns to sum must only contain numbers and not strings --- alternatively use a pageSummaryFunc to build your own custom function to summarize.

newadventure079 commented 2 months ago

If anyone else comes across this and sees the documentation and wonders how you can use a function to summarize when the docs say your options for pageSummaryFunc are only a string, the source code gives more details.

/**
     * @var string|Closure the summary function that will be used to calculate the page summary for the column. If
     * setting as `Closure`, you can set it to an anonymous function with the following signature:
     *
     * ```php
     * function ($data)
     * ```
     *
     *   - the `$data` variable will contain array of the selected page rows for the column.
     */