/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate($id) { $regions = Region::getRegionList(); $model = new Ad(); $model->attachEavSet(Category::model()->findByPk($id)->set_id); $model->category_id = $id; $photo = new Photo(); if (isset($_POST['Ad'])) { $model->attributes = $_POST['Ad']; $model->author_id = Yii::app()->user->id; $transaction = Yii::app()->db->beginTransaction(); if ($model->saveWithEavAttributes()) { $images = CUploadedFile::getInstancesByName('images'); if ($images) { $wrongImage = Photo::validateMultiple($images, $model->id); if (!$wrongImage) { foreach ($images as $image) { $photo = new Photo(); $photo->image = $image; $photo->name = $photo->image->getName(); $photo->ad_id = $model->id; $photo->save(false); } $transaction->commit(); $this->redirect(array('view', 'id' => $model->id)); } else { $photo = $wrongImage; $transaction->rollback(); } } else { $transaction->commit(); SphinxService::saveAdToRt($model->id); $this->redirect(array('view', 'id' => $model->id)); } } } $this->render('create', array('model' => $model, 'photo' => $photo, 'regions' => $regions)); }