1

I need to assign machines to a machine group (m:n). The mount of machines makes it necesary to use a gridview with checkboxcolumn to assign machines to a group. I got it to work that the relations are getting saved but I didn't figured it out how to make allready assigned machines to be checked in the gridview when it gets loaded. The content of my gridview is a dataprovider based on a MySQL-View. With

 'checkboxOptions' => function($model, $key, $index, $column) {
     return ['checked' => true];
 }

it's possible to check all the checkboxes. But when I'm trying to do this

 'checkboxOptions' => function($model, $key, $index, $column) {
     $bool = in_array($model->id_machine, common\models\MachineGroup::getAssignedMachines());
     return ['checked' => $bool];
 }

an error is thrown: "Cannot use object of type yii\web\View as array". Actually I don't understand what's the problem here but I couldn't find a way to pass the array of the allready selected machines to this funtion (and I tried a lot). When I define a dummy-array manually in the function everything works fine. Need some help here...thanks!

Chris vD
  • 73
  • 2
  • 12
  • When the error occurs, the line with `$bool = in_array($model->id_machine, common\models\MachineGroup::getAssignedMachines());` is highlighted in error message ? – Tony Jul 01 '15 at 03:13
  • no...here's the error message: – Chris vD Jul 01 '15 at 03:28
  • 1. in C:\xampp\htdocs\eddb1\vendor\yiisoft\yii2\db\ActiveRelationTrait.php at line 458 – Chris vD Jul 01 '15 at 03:32
  • the highlighted line is: "if (($value = $model[$attribute]) !== null) {" – Chris vD Jul 01 '15 at 03:33
  • maybe there is something in `common\models\MachineGroup::getAssignedMachines()` method ? can you add its code to the question ? – Tony Jul 01 '15 at 04:03
  • this code is working normal and is running for different views...also works if I call it in this view, but not inside the checkboxOptions-function. I think it has something to do with the object "checkboxOptions"...no idea :-( – Chris vD Jul 01 '15 at 04:11
  • did you tried `$machines = common\models\MachineGroup::getAssignedMachines();` and then in callback `function($model, $key, $index, $column) use ($machines) {$bool = in_array($model->id_machine, $machines); return ['checked' => $bool];}` – Tony Jul 01 '15 at 04:19
  • yeahhhhh!!!! that worked! Thank you lot Tony...so happy cause I tried for the whole day now...didn't knew the hack with use, allways tried to implement another function-parameter and change the vendor-source-code but obviously without success. Thank you again Tony! – Chris vD Jul 01 '15 at 04:32

2 Answers2

3

As i wrote in comments, define variable outside of callback

$machines = common\models\MachineGroup::getAssignedMachines();

and then in callback function

function($model, $key, $index, $column) use ($machines) {
    $bool = in_array($model->id_machine, $machines); 
    return ['checked' => $bool];
}
Tony
  • 5,797
  • 3
  • 26
  • 22
  • hello Tony, are you out there? I published another question regarding [render after post request](http://stackoverflow.com/questions/31272381/yii2-render-after-post-request) but no one can answer my question. Please help, got stuck for one week now – Chris vD Jul 08 '15 at 20:19
  • Thanks Tony for this tip. Was searching for it. – Fuad All Sep 01 '19 at 20:44
0

In Yii2 GridView you can prepare data to check in checkboxcolumn by use keyword to use variable to anonymous function and use in_array() to check if correct checkbox is checked. This is example.

//outside gridview prepare data list for check
$ar = [];
foreach(Model::find()->select(['id'])->asArray()->all() as $key => $val){
    $ar[] = $val['id'];
}

//..... inside checkbox column
    'checkboxOptions' => function($model, $key, $index, $column) use ($ar) {

             $bool = in_array($model->id, $ar);
             return ['checked' => $bool];
    }
//.....
  • Hi Manop Kongoon; your code might be correct, but with some context it would make a better answer; for example, you could explain how and why this proposed change would resolve the questioner's problem, perhaps including a link to the relevant documentation. That would make it more useful to them, and also more useful to other site readers who are looking for solutions to similar problems. – Vince Bowdren Aug 19 '16 at 16:01