Error in complex forms

Hello,

So I have a form that has controls for both companies and branches.

My create view and controller works great!

But when I try and update a company I get the following error in the view:

Call to a member function formName() on a non-object

I suspect this code in the actionUpdate is the culprit (full code below):


$branch = $model->branches;

CompaniesControler update code:


/**

     * Updates an existing Companies model.

     * If update is successful, the browser will be redirected to the 'view' page.

     * @param integer $id

     * @return mixed

     */

    public function actionUpdate($id)

    {

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

        $branch = $model->branches;


        if ($model->load(Yii::$app->request->post()) && $branch->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->company_id]);

        } else {

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

                'model' => $model,

                'branch' => $branch,

            ]);

        }

    }

Comnies Update view:


<div class="companies-update">


    <h1><?= Html::encode($this->title) ?></h1>


    <?= $this->render('_form', [

        'model' => $model,

        'branch' =>$branch,

    ]) ?>


</div>

companies _form.php (this works fine in create)


<?php


use yii\helpers\Html;

use yii\widgets\ActiveForm;

use dosamigos\datepicker\DatePicker;


/* @var $this yii\web\View */

/* @var $model backend\models\Companies */

/* @var $form yii\widgets\ActiveForm */

?>


<div class="companies-form">


    <?php $form = ActiveForm::begin(['enableAjaxValidation'=>true]); ?>


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


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


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

    

    <?= $form->field($model, 'company_status')->dropDownList([ 'active' => 'Active', 'inactive' => 'Inactive', ], ['prompt' => 'Satus']) ?>


    <?= $form->field($model, 'company_start_date')->widget(

    DatePicker::className(), [

        // inline too, not bad

        'inline' => true, 

        // modify template for custom rendering

        'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',

        'clientOptions' => [

            'autoclose' => true,

            'format' => 'dd-M-yyyy'

        ]

]);?>

    

    <!-- Create branch for this company -->


    <?= $form->field($branch, 'branch_name')->textInput(['maxlength' => 100]) ?>


    <?= $form->field($branch, 'branch_address')->textInput(['maxlength' => 255]) ?>


    <?= $form->field($branch, 'branch_status')->dropDownList([ 'active' => 'Active', 'inactive' => 'Inactive', ], ['prompt' => 'Status']) ?>


    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>


    <?php ActiveForm::end(); ?>


</div>

Ok, change this:


$branch = $model->branches;

to this


$branch = $model->branches[0];

and now it works.

This is a 1=1 relationship so this is ok.