public function afterSave($insert, $changedAttributes) { if (!parent::afterSave($insert, $changedAttributes)) { return false; } $boardCounter = new BoardCounter(['boardId' => $this->id]); $boardCounter->save(); }
public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); if ($insert) { $tagsArray = Tag::parse($this->postData->messageText); foreach ($tagsArray as $tag) { $this->link('tags', $tag); } } }
public function create(string $boardName, array $data) { /** @var Board $board */ if (!($board = Board::findOne(['name' => $boardName]))) { throw new UserException('Board was not found', 404); } $validationParams = $board->postsSettings->getValidationParams(); $thread = new Thread(['boardId' => $board->id]); $postData = new PostData(['validationParams' => $validationParams]); $uploadForm = new UploadForm(['files' => PostedFile::getPostedFiles($validationParams['maxFiles']), 'validationParams' => $validationParams]); $toLoad = [$thread, $postData]; $toValidate = [$thread, $postData, $uploadForm]; if (!ARExtended::loadMultiple($toLoad, $data) || !Model::validateMultiple($toValidate)) { throw new UserException('Failed to load/validate data', 400); } if (strlen($postData->messageText) == 0 && count($uploadForm->files)) { throw new UserException('Must be attached atleast 1 file or non-empty message', 400); } $toSave = [$postData, $thread, $uploadForm]; $postData->on(PostData::EVENT_AFTER_INSERT, function ($event) use($thread) { $thread->postDataId = $event->sender->id; }); $saved = true; foreach ($toSave as $model) { if (method_exists($model, 'save')) { $saved = $saved && $model->save(); if (!$saved) { break; } } } if (!$saved) { for ($i = count($toSave) - 1; $i >= 0; $i--) { method_exists($toSave[$i], 'delete') && $toSave[$i]->delete(); } throw new UserException('Failed to create thread', 500); } foreach ($uploadForm->getFileInfos() as $fileInfo) { $postData->link('fileInfos', $fileInfo); } $thread->link('board', $board); return $thread; }
public function beforeDelete() { unlink($this->getThumbPath()); return parent::beforeDelete(); }
public function beforeSave($insert) { if (parent::beforeSave($insert)) { if ($this->isNewRecord) { $this->authKey = \Yii::$app->security->generateRandomString(); } return true; } return false; }
public function save($runValidation = true, $attributeNames = null) { $isFilePathChanged = $this->getOldAttribute('file_path') !== $this->filePath; if (!parent::save($runValidation, $attributeNames)) { return false; } if (!$isFilePathChanged) { return true; } $subClassInstance = $this->getSubClassInstance(); if (!is_null($subClassInstance)) { $subClassInstance->delete(); } $SubClass = $this->getSubClassName(); if (is_null($SubClass)) { parent::delete(); throw new ErrorException('File subclass was not found'); return false; } $subClassInstance = new $SubClass(['filesInfoId' => $this->id]); if ($subClassInstance->save()) { $subClassInstance->link('fileInfo', $this); } else { print_r('subclass instance was not saved\\n'); parent::delete(); return false; } return true; }