Updating a record using two models at the same time


I want to update a record using two models but I’m getting this error when trying to do just that:

My actionUpdate in the controller:

public function actionUpdate($id)


        $this->layout = 'detail';

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

        $modelGpcc = GPCCCAMPAIGNS::findOne($id);

        $modelSat = CAMPDETALLENOTIFICACIONESSAT::findOne($id);

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

            $modelSat->CODIGO = $model->CODIGO;

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

                $session = Yii::$app->session;

                Yii::$app->session->setFlash('kv-detail-success', 'Datos guardados con éxito!');

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


        } else {

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

                'model' => $model,

                'modelGpcc' => $modelGpcc,

                'modelSat' => $modelSat,




and protected function findmodeltwo

protected function findModeltwo($id)


        if (($model = CAMPDETALLENOTIFICACIONES::findOne($id)) !== null) {

            return $model;

        } else {

            throw new NotFoundHttpException('The requested page does not exist.');



I really don’t know what to do for this to work :(

can you post the stack trace

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

        $modelSat = CAMPDETALLENOTIFICACIONESSAT::findOne($id);

either of these two will be returning null.

Of course!

Call to a member function formName() on null

1. in C:\xampp\htdocs\gpcc-v1-dev\vendor\yiisoft\yii2\helpers\BaseHtml.php at line 2096


    /* See [[getAttributeName()]] for explanation of attribute expression.


     * @param Model $model the model object

     * @param string $attribute the attribute name or expression

     * @return string the generated input name

     * @throws InvalidParamException if the attribute name contains non-word characters.


    public static function getInputName($model, $attribute)


        $formName = $model->formName(); // THE RED LINE IS HERE (line 2096)

        if (!preg_match('/(^|.*\])([\w\.]+)(\[.*|$)/', $attribute, $matches)) {

            throw new InvalidParamException('Attribute name must contain word characters only.');


        $prefix = $matches[1];

        $attribute = $matches[2];

        $suffix = $matches[3];

        if ($formName === '' && $prefix === '') {

            return $attribute . $suffix;

        } elseif ($formName !== '') {

2. yii\base\ErrorHandler::handleFatalError()

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

        $modelGpcc = GPCCCAMPAIGNS::findOne($id);

        $modelSat = CAMPDETALLENOTIFICACIONESSAT::findOne($id);

one of the above is returning null causing the form to blow up, it is likely that you don’t have that record in the database do a simple if condition to check before rendering the form input

Thanks hrnair and alirz23 that was it! :D