Example #1
0
 /**
  * Finds the AdCategory model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param string $id
  * @return AdCategory the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = AdCategory::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #2
0
 /**
  * @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;
 }