There is a table of deal, which consists of id, name, connect_deal. The last column contains the id of the related row of the same table, if any. To remove values ​​from connect_deal, I run the following code:

$deal = Deal::findOne($id); $conId = $deal->connectDeal; $deal->connectDeal = null; if ($deal->save()) { $upDeal = Deal::findOne($conId); $upDeal->connectDeal = null; $result = $upDeal->save(); if ($result) { Yii::$app->session->setFlash('success', 'Связь успешно удалена'); return $this->refresh(); } } .... 

As a result, the data is deleted, as I need, but it turns out an error, the debugger allocates the line $upDeal->connectDeal = null; and displays the warning Creating default object from empty value , while $deal->connectDeal = null does not cause any questions to the program. What could be the error and how to solve it?

    1 answer 1

    The error occurs because you are trying to assign the connectDeal property connectDeal nonexistent object. In other words, $upDeal = Deal::findOne($conId); gives out null in certain situations. For example, when there was no connection, or a coherent record in the table does not exist.

    Add a check for the 2nd Deal::findOne($conId)

      $deal = Deal::findOne($id); $conId = $deal->connectDeal; $deal->connectDeal = null; if ($deal->save() && ($upDeal = Deal::findOne($conId)) !== null) { $upDeal->connectDeal = null; $result = $upDeal->save(); if ($result) { Yii::$app->session->setFlash('success', 'Связь успешно удалена'); return $this->refresh(); } }