public function typeGif($attributes, $urls) { $post = new Gif(); $post->setScenario($post::SCENARIO_INSERT); foreach ($urls as $url) { $file = new Files(); $file->url = $url; $file->name = basename($url); if ($file->validate()) { $post->uploadFiles[] = $file; } } $post->setAttributes($attributes); $post->provider = $this->providerName; $post->status = Gif::STATUS_NEW; $post->setAttribute('type', $post->type); if (!$post->validate()) { return null; } return $post; }
public function upload() { if ($this->validate()) { foreach ($this->files as $file) { $file->delete(); } foreach ($this->uploadFiles as $file) { $fileModel = new Files(); $fileModel->name = $file->name; $fileModel->url = $fileModel->fileUrl; $file->saveAs($fileModel->filePath); if (!$fileModel->save()) { $fileModel->stopTransaction(); return false; } $postFile = new PostFile(); $postFile->post_id = $this->id; $postFile->file_id = $fileModel->id; if (!$postFile->validate() || !$postFile->save()) { $postFile->stopTransaction(); return false; } } return true; } else { return false; } }
/** * @return \yii\db\ActiveQuery */ public function getFiles() { return $this->hasMany(Files::className(), ['id' => 'file_id'])->viaTable('post_file', ['post_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getFile() { return $this->hasOne(Files::className(), ['id' => 'file_id']); }