5

I am trying to access a variable on view file into Gridview but it is throwing error that it should be array but null given i am declaring $totalDays on top of my view file and i am using it in Gridview like below

[
            'attribute' =>  'class_id',
            'format' => 'raw',
            'label' => "Class Date",
            'value' => function ($model) {
                array_push($totalDays,$model->class->date);
                return $model->class->date;
            },
            'footer'=> '<span>Total Days</span>',
],

But it throws following error

array_push() expects parameter 1 to be array, null given

Insane Skull
  • 9,220
  • 9
  • 44
  • 63
Mike Ross
  • 2,942
  • 5
  • 49
  • 101
  • This will answer your question: https://stackoverflow.com/questions/31241336/yii2-pass-variable-from-view-to-gridview-custom-action-columns – Laith Sep 12 '17 at 08:09

4 Answers4

5

Insert use after function signature in value callable function:

[
            'attribute' =>  'class_id',
            'format' => 'raw',
            'label' => "Class Date",
            'value' => function ($model) use($totaleDays) {
                array_push($totalDays,$model->class->date);
                return $model->class->date;
            },
            'footer'=> '<span>Total Days</span>',
],
Fabrizio Caldarelli
  • 2,982
  • 11
  • 14
5

To explain, $totalDays is not available in the widget because the whole function only gets run when the widget is rendered, $totalDays is no longer declared. As @arogachev hinted above, you will need to make $totalDays in your model, then you can access it. Try this in your model;

public function getTotalDays(){
//Your logic here to generate totalDays
return $totalDays;
}

Then you can use it in your view like this;

[
    'attribute' =>  'class_id',
    'format' => 'raw',
    'label' => "Class Date",
    'value' => function ($model) {
        array_push($model->totalDays, totalDays,$model->class->date);
        return $model->class->date;
    },
    'footer'=> '<span>Total Days</span>',
],
Joe Miller
  • 3,843
  • 1
  • 23
  • 38
3

As @Fabrizio said. Insert use after function signature in value callable function:

'value' => function ($model) use($totaleDays) {
    array_push($totalDays,$model->class->date);
    return $model->class->date;
},

For multiple value to pass in to value function you can use as below.

'value' => function ($model) use($var1, $var2....., $varN) {
    array_push($totalDays,$model->class->date);
    return $model->class->date;
},

Like this use($var1, $var2....., $varN)

Sahil Patel
  • 1,570
  • 2
  • 15
  • 34
2

While you can definetely pass this variable through use section of closure, it's wrong in your code. MVC principle is violated, because view it's only for display, and you exposing logic such as array_push.

I'd recommend to refactor it and place data calculation in model, so you can simply call return $model->yourMethod(); and it will return desired data.

arogachev
  • 33,150
  • 7
  • 114
  • 117
  • ok. So you suggest that i should not use functions on the view page and instead just call it from controller or model... – Mike Ross Dec 28 '15 at 22:25