M
M
Michael2019-08-01 22:39:05
Yii
Michael, 2019-08-01 22:39:05

How to fix "Call to a member function isAttributeRequired() on null" error when opening linked tables form?

Hello everybody! Tell me how to fix the problem, there are two interconnected tables Accept and Address. For understanding, data about the object is written to the Accept table, and data about the location of the object is written to the Address table. Saving data from the form to the database is fine, editing a new added record is also fine. The problem is that I need to edit previously added records in the Accept table, for which there is no information about the objects in the Address table yet, and when I open such an object, I get the following error:

Call to a member function isAttributeRequired() on null

update action
public function actionUpdate($id)
{
    $model = Accept::find()->where(['id'=>$id])->one();
    $address = Address::find()->where(['accept_id'=>$model->id])->one();

    if ($model->load(Yii::$app->request->post()) && $address->load(Yii::$app->request->post())) {
        if($model->save()){
            $address->accept_id = $model->id;
            if ($address->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }
    }
    return $this->render('update', compact('model', 'address'));
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question