yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.23k stars 6.91k forks source link

GridView::widget error array_merge(): Argument #2 is not an array #14818

Closed arthurkushman closed 7 years ago

arthurkushman commented 7 years ago

What steps will reproduce the problem?

by passing the object to GridView::widget method like this:

\yii\grid\GridView::widget(
                [
                    'layout'       => "{items}\n{pager}",
                    'dataProvider' => $list,
                    'columns'      => $attributes
                ]
            );

with $attributes not only the array, but an object. Check algorithm can detect an array, but can not an object and crushes.

What is the expected result?

The grid table should be full-filled and rendered on the page

What do you get instead?

An error

2017-09-14 15:56:51 [127.0.0.1][kushmantsev_a][2856fptkvjppcmlsukur88g63t][error][yii\base\ErrorException:2] yii\base\ErrorException: array_merge(): Argument #2 is not an array in /
Users/arthur/www/qcs/vendor/yiisoft/yii2/grid/GridView.php:534
Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleError(2, 'array_merge(): ...', '/Users/arthur/w...', 534, Array)
#1 /Users/arthur/www/qcs/vendor/yiisoft/yii2/grid/GridView.php(534): array_merge(Array, Object(Closure))
#2 /Users/arthur/www/qcs/vendor/yiisoft/yii2/grid/GridView.php(278): yii\grid\GridView->initColumns()
#3 /Users/arthur/www/qcs/vendor/yiisoft/yii2/base/BaseObject.php(108): yii\grid\GridView->init()
#4 [internal function]: yii\base\BaseObject->__construct(Array)
#5 /Users/arthur/www/qcs/vendor/yiisoft/yii2/di/Container.php(382): ReflectionClass->newInstanceArgs(Array)
#6 /Users/arthur/www/qcs/vendor/yiisoft/yii2/di/Container.php(156): yii\di\Container->build('yii\\grid\\GridVi...', Array, Array)
#7 /Users/arthur/www/qcs/vendor/yiisoft/yii2/BaseYii.php(349): yii\di\Container->get('yii\\grid\\GridVi...', Array, Array)
#8 /Users/arthur/www/qcs/vendor/yiisoft/yii2/base/Widget.php(137): yii\BaseYii::createObject(Array)
#9 /Users/arthur/www/qcs/views/statistic/survey-table.php(80): yii\base\Widget::widget(Array)
#10 /Users/arthur/www/qcs/vendor/yiisoft/yii2/base/View.php(330): require('/Users/arthur/w...')
#11 /Users/arthur/www/qcs/vendor/yiisoft/yii2/base/View.php(250): yii\base\View->renderPhpFile('/Users/arthur/w...', Array)
#12 /Users/arthur/www/qcs/vendor/yiisoft/yii2/base/View.php(152): yii\base\View->renderFile('/Users/arthur/w...', Array, Object(app\controllers\StatisticController))
#13 /Users/arthur/www/qcs/vendor/yiisoft/yii2/base/Controller.php(384): yii\base\View->render('survey-table', Array, Object(app\controllers\StatisticController))
#14 /Users/arthur/www/qcs/controllers/StatisticController.php(172): yii\base\Controller->render('survey-table', Array)
#15 [internal function]: app\controllers\StatisticController->actionSurveyTable()
#16 /Users/arthur/www/qcs/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#17 /Users/arthur/www/qcs/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#18 /Users/arthur/www/qcs/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('survey-table', Array)
#19 /Users/arthur/www/qcs/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('statistic/surve...', Array)
#20 /Users/arthur/www/qcs/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(app\components\Request))
#21 /Users/arthur/www/qcs/web/index.php(16): yii\base\Application->run()
#22 {main}

Additional info

Q A
Yii version 2.0.6
PHP version 7.1.6
Operating system MacOSX
alex-code commented 7 years ago

What is your $attributes variable?

arthurkushman commented 7 years ago

What is what? Content or You need the print_r here? It is an array 100%, because there is a check with is_array($attributes) code:

        if (empty($attributes) === false && is_array($attributes)) {
            echo \yii\grid\GridView::widget(
                [
                    'layout'       => "{items}\n{pager}",
                    'dataProvider' => $list,
                    'columns'      => $attributes
                ]
            );
        }
alex-code commented 7 years ago

It might be an array itself but what does it contain?

arthurkushman commented 7 years ago

That's what I've talked about, ok - here it is:

Array ( [0] => Array ( [class] => yii\grid\SerialColumn ) [attribute] => errorContainedPercent [format] => raw [content] => Closure Object ( [this] => yii\web\View Object ( [assetBundles] => Array ( [app\views\statistic\StatisticAsset] => app\views\statistic\StatisticAsset Object ( [sourcePath] => /Users/arthur/www/qcs/views/statistic/assets [css] => Array ( [0] => style.css ) [js] => Array ( ) [basePath] => /Users/arthur/www/qcs/web/assets/a3dfc438 [baseUrl] => /assets/a3dfc438 [depends] => Array ( ) [jsOptions] => Array ( ) [cssOptions] => Array ( ) [publishOptions] => Array ( ) ) [app\components\widgets\FilterWidgetAsset] => app\components\widgets\FilterWidgetAsset Object ( [sourcePath] => /Users/arthur/www/qcs/components/widgets/assets [css] => Array ( [0] => filter-widget-style.css ) [basePath] => /Users/arthur/www/qcs/web/assets/c68041a4 [baseUrl] => /assets/c68041a4 [depends] => Array ( ) [js] => Array ( ) [jsOptions] => Array ( ) [cssOptions] => Array ( ) [publishOptions] => Array ( ) ) [yii\web\JqueryAsset] => yii\web\JqueryAsset Object ( [sourcePath] => /Users/arthur/www/qcs/vendor/bower/jquery/dist [js] => Array ( [0] => jquery.js ) [basePath] => /Users/arthur/www/qcs/web/assets/1ca7002f [baseUrl] => /assets/1ca7002f [depends] => Array ( ) [css] => Array ( ) [jsOptions] => Array ( ) [cssOptions] => Array ( ) [publishOptions] => Array ( ) ) [kartik\date\DatePickerAsset] => kartik\date\DatePickerAsset Object ( [depends] => Array ( [0] => yii\web\JqueryAsset [1] => yii\bootstrap\BootstrapAsset ) [js] => Array ( [0] => js/bootstrap-datepicker.min.js [1] => js/datepicker-kv.min.js [2] => js/locales/bootstrap-datepicker.ru.min.js [3] => js/locales/bootstrap-datepicker.ru.min.js ) [css] => Array ( [0] => css/bootstrap-datepicker3.min.css [1] => css/datepicker-kv.min.css ) [sourcePath] => /Users/arthur/www/qcs/vendor/kartik-v/yii2-widget-datepicker/assets [basePath] => /Users/arthur/www/qcs/web/assets/15e73389 [baseUrl] => /assets/15e73389 [jsOptions] => Array ( ) [cssOptions] => Array ( ) [publishOptions] => Array ( ) ) [yii\bootstrap\BootstrapAsset] => yii\bootstrap\BootstrapAsset Object ( [sourcePath] => /Users/arthur/www/qcs/vendor/bower/bootstrap/dist [css] => Array ( [0] => css/bootstrap.css ) [basePath] => /Users/arthur/www/qcs/web/assets/474af58b [baseUrl] => /assets/474af58b [depends] => Array ( ) [js] => Array ( ) [jsOptions] => Array ( ) [cssOptions] => Array ( ) [publishOptions] => Array ( ) ) [kartik\base\WidgetAsset] => kartik\base\WidgetAsset Object ( [depends] => Array ( [0] => yii\web\JqueryAsset [1] => yii\bootstrap\BootstrapAsset ) [js] => Array ( [0] => js/kv-widgets.min.js ) [css] => Array ( [0] => css/kv-widgets.min.css ) [sourcePath] => /Users/arthur/www/qcs/vendor/kartik-v/yii2-krajee-base/assets [basePath] => /Users/arthur/www/qcs/web/assets/9bcfef1d [baseUrl] => /assets/9bcfef1d [jsOptions] => Array ( ) [cssOptions] => Array ( ) [publishOptions] => Array ( ) ) [yii\widgets\ActiveFormAsset] => yii\widgets\ActiveFormAsset Object ( [sourcePath] => /Users/arthur/www/qcs/vendor/yiisoft/yii2/assets [js] => Array ( [0] => yii.activeForm.js ) [depends] => Array ( [0] => yii\web\YiiAsset ) [basePath] => /Users/arthur/www/qcs/web/assets/4c23d013 [baseUrl] => /assets/4c23d013 [css] => Array ( ) [jsOptions] => Array ( ) [cssOptions] => Array ( ) [publishOptions] => Array ( ) ) [yii\web\YiiAsset] => yii\web\YiiAsset Object ( [sourcePath] => /Users/arthur/www/qcs/vendor/yiisoft/yii2/assets [js] => Array ( [0] => yii.js ) [depends] => Array ( [0] => yii\web\JqueryAsset ) [basePath] => /Users/arthur/www/qcs/web/assets/4c23d013 [baseUrl] => /assets/4c23d013 [css] => Array ( ) [jsOptions] => Array ( ) [cssOptions] => Array ( ) [publishOptions] => Array ( ) ) ) [title] => Статистика чек-листа (табличный вид) [metaTags] => Array ( ) [linkTags] => Array ( ) [css] => Array ( ) [cssFiles] => Array ( ) [js] => Array ( [4] => Array ( 

The overall content is much bigger, so the above is an example of upper content.

As I said, in structure there are objects which breaks GridView functionality.

alex-code commented 7 years ago

How have you constructed the $attributes array? The first bit you've shown doesn't look right to me.

[
  [
   'class' => 'yii\grid\SerialColumn'
  ],
  'attribute' => 'errorContainedPercent',
  'format' => 'raw '
]
arthurkushman commented 7 years ago

I've found the answer now, sorry for disturbance - I'm closing this thread.