I read the documentation I want to bring the code to a normal form.
/** * Display forms adding the sell offer * * @param integer $id orders * @return mixed * @throws HttpException */ public function actionOfferSellForm($id = null) { $modelOfferBuyForm = new OfferBuyForm(); $order = Order::findOne($id); if (!$order) { throw new \yii\web\HttpException(404, 'Page not found'); } $certificate = Certificate::findOne(['id' => $order->source_id]); if (!$certificate) { throw new \yii\web\HttpException(404, 'Page not found'); } if ($order->user_id == Yii::$app->user->identity->id) { return $this->renderAjax('/site/info', ['message' => 'Вы не можете приобрести свой сертификат']); } $modelOfferBuyForm->source_id = $order->id; $modelOfferBuyForm->certificate_code = $certificate->certificate_code; $modelOfferBuyForm->buyer_id = Yii::$app->user->identity->id; $modelOfferBuyForm->seller_id = $order->user_id; if ($modelOfferBuyForm->load(Yii::$app->request->post()) && $modelOfferBuyForm->validate()) { $modelOfferBuyForm->status = 1; $modelOfferBuyForm->expertise = 0; $modelOfferBuyForm->priced_currency = $order->priced_currency; if ($modelOfferBuyForm->save()) { Yii::$app->session->setFlash('success', 'Заявка отправленна.'); return $this->redirect('index'); } else { Yii::$app->session->setFlash('error', 'Ошибка отправки заявки.'); } } elseif (Yii::$app->request->isAjax) { return $this->renderAjax('offerBuyForm', ['model' => $modelOfferBuyForm]); } else { return $this->render('offerBuyForm', ['model' => $modelOfferBuyForm]); } } This code takes a Transaction and searches for a certificate, after which it performs manipulations. I want to make it more elegant, since if (!$order) { throw new \yii\web\HttpException(404, 'Page not found'); } $certificate = Certificate::findOne(['id' => $order->source_id]); if (!$certificate) { throw new \yii\web\HttpException(404, 'Page not found'); } if (!$order) { throw new \yii\web\HttpException(404, 'Page not found'); } $certificate = Certificate::findOne(['id' => $order->source_id]); if (!$certificate) { throw new \yii\web\HttpException(404, 'Page not found'); } if (!$order) { throw new \yii\web\HttpException(404, 'Page not found'); } $certificate = Certificate::findOne(['id' => $order->source_id]); if (!$certificate) { throw new \yii\web\HttpException(404, 'Page not found'); } I think not the best practice. well and other moments like i f ($modelOfferBuyForm->save()) { without validation