/** * Finds the AdMain model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return AdMain the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = AdMain::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Finds the AdRealEstate model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return AdRealEstate the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModelAdMain($id) { if (($modelAdMain = AdMain::findOne($id)) !== null) { return $modelAdMain; } else { //throw new NotFoundHttpException('The requested page does not exist.'); return $this->redirect(['/ad/default/index']); } }
/** * @param $modelAdRealEstate * @return AdRealEstate * @throws Exception */ public function saveAd($modelAdRealEstate) { /* @var $modelAdRealEstate \common\models\AdRealEstate */ //$modelAdRealEstate->temp = 1; $transaction = Yii::$app->db->beginTransaction(); try { if ($modelAdRealEstate->save()) { if (isset(Yii::$app->request->post('AdRealEstate')['appliances'])) { $modelAdRealEstate->appliances = Yii::$app->request->post('AdRealEstate')['appliances']; AdRealEstateAppliances::deleteAll(['real_estate_id' => $modelAdRealEstate->id]); foreach ($modelAdRealEstate->appliances as $one) { $modelAdRealEstateAppliances = new AdRealEstateAppliances(); $modelAdRealEstateAppliances->real_estate_id = $modelAdRealEstate->id; $modelAdRealEstateAppliances->reference_id = $one; $modelAdRealEstateAppliances->save(); } } $modelCategory = $modelAdRealEstate->adCategory ? $modelCategory = AdCategory::findOne($modelAdRealEstate->adCategory->id) : new AdCategory(); $modelCategory->category = 1; // Категория для недвижемость 1 (из reference main) $modelCategory->ad_id = $modelAdRealEstate->id; if ($modelCategory->save()) { $modelAdMain = ($modelAdMain = AdMain::findOne(['category_id' => $modelCategory->id])) ? $modelAdMain : new AdMain(); $modelAdMain->user_id = Yii::$app->user->id; $modelAdMain->place_city_id = $this->place_city_id; $modelAdMain->category_id = $modelCategory->id; $modelAdMain->phone_temp_ad = $this->phone_temp_ad; $modelAdMain->link_temp_ad = $this->link_temp_ad; //$modelAdMain->ad_style_id = 1; if ($modelAdMain->save()) { $transaction->commit(); } } } else { \Yii::$app->session->set('error', 'Объявление недвижимости не добавлено.'); // если все в порядке, пишем в сессию путь к изображениею } } catch (Exception $e) { $transaction->rollBack(); } return $modelAdRealEstate; }