public function create() { $isValid = false; $this->imageFile = UploadedFile::getInstance($this, 'imageFile'); $user = User::findOne(Yii::$app->user->getId()); $bulletin = new Bulletin(); if (isset($this->imageFile)) { $imagePath = '/uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension; $this->imageFile->saveAs(Yii::getAlias('@webroot') . $imagePath); $m_image = new Image(); $m_image->url = $imagePath; if ($m_image->save()) { $image = Image::findOne($m_image->getPrimaryKey()); $bulletin->link('image', $image); $bulletin->link('user', $user); $isValid = true; } else { $isValid = false; } } $bulletin->title = $this->title; $bulletin->description = $this->description; if ($bulletin->save()) { $isValid = true; } else { $isValid = false; } return $isValid; }
/** * Creates a new Work model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $workModel = new Work(); $metaModel = new MetaTag(); $image1Model = new Image(); $image1Model->setScenario('add'); $image2Model = new Image(); $transaction = null; try { if (!$workModel->load(Yii::$app->request->post())) { throw new Exception(); } $transaction = \yii::$app->getDb()->beginTransaction(); $image1Storage = new ImageStorage($image1Model); $image2Storage = new ImageStorage($image2Model); if ($image1Storage->isSendFile('image1') && $image1Storage->save()) { $image1Model->save(); $workModel->image1 = $image1Model->id; } if ($image2Storage->isSendFile('image2') && $image2Storage->save()) { $image2Model->save(); $workModel->image2 = $image2Model->id; } if (!$workModel->save()) { throw new Exception(); } $metaModel->load(Yii::$app->request->post()); $metaModel->link = '/works/details/' . $workModel->id; if (!$metaModel->save()) { throw new Exception(); } $transaction->commit(); return $this->redirect(['index']); } catch (Exception $e) { if (isset($image1Storage)) { $image1Storage->delete(); } if (isset($image2Storage)) { $image2Storage->delete(); } if ($transaction instanceof Transaction) { $transaction->rollBack(); } } return $this->render('create', ['workModel' => $workModel, 'metaModel' => $metaModel, 'image1Model' => $image1Model, 'image2Model' => $image2Model]); }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); if ($user->save()) { $image = new Image(); $image->url = "/uploads/none-avatar.png"; $image->save(); $profile = new Profile(); $profile->link('user', $user); $profile->link('image', $image); $profile->save(); return $user; } } return null; }
public function save() { if ($this->validate()) { $isValid = false; $this->imageFile = UploadedFile::getInstance($this, 'imageFile'); $user = User::findOne(Yii::$app->user->getId()); if (isset($user->profile)) { $profile = Profile::findOne($user->profile->id); } else { $profile = new Profile(); } if (isset($this->imageFile)) { $imagePath = '/uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension; $this->imageFile->saveAs(Yii::getAlias('@webroot') . $imagePath); $m_image = new Image(); $m_image->url = $imagePath; if ($m_image->save()) { $image = Image::findOne($m_image->getPrimaryKey()); $profile->link('image', $image); $isValid = true; } else { $isValid = false; } } $profile->firstname = $this->firstname; $profile->lastname = $this->lastname; $profile->aboutme = $this->aboutme; $profile->link('user', $user); if ($profile->save()) { $isValid = true; } else { $isValid = false; } return $isValid; } else { return false; } }