Multiple models and checkboxList

Hello.

I am confused about how to use checkboxList with multiple models.

Let me explain it better…

I have the Fiscalizacao model and Agenda model.

One Fiscalizacao can have many Agenda, so I have a third model called AgendaFiscalizacao.

My Update Action:




public function actionUpdate($id)

{

    $model = $this->findModel($id);

    $modelAgenda = AgendaFiscalizacao::findAll(['fiscalizacao_id' => $id]);


    if ($model->load(Yii::$app->request->post()) && Model::loadMultiple($modelAgenda, Yii::$app->request->post())) {

      // No problem here yet......

    }


    return $this->render('update', [

        'model' => $model,

        'modelAgenda' => $modelAgenda

    ]);

}



My form:




<?= $form->field($modelAgenda, 'agenda_id')->checkboxList(Agenda::combo(), ['class' => 'checkbox']) ?>

<?= $form->field($model, 'bioma_id')->dropDownList(Bioma::combo(), ['prompt' => $prompt]) ?>

<?= $form->field($model, 'nome')->textInput(['maxlength' => true]) ?>



My problem:

My actionCreate is working properly, saving into agenda_fiscalizacao table.

But in actionUpdate i’m receiving this error that prevent the form to load:

Call to a member function formName() on array

What could be wrong?

Thanks and sorry for my bad english!

$modelAgenda will return array of object(multiple), so you should render the field using the for loop with indexed value, may be this will help you… The related model fields should be with in the foreach look


foreach ($modelAgenda as $i => $agenda) {

    echo $form->field($agenda, "[$i]agenda_id"->checkboxList(Agenda::combo(), ['class' => 'checkbox']);

}

I just wrote a wiki on how to use listBox and checkboxList.

Please take a look at it.

http://www.yiiframework.com/wiki/836/how-to-use-listbox-and-checkboxlist/