public function saveLogo($path) { $attachment = new Attachment(); $attachment->uploadFromFile($path); $attachment->save(); $attachmentIndex = new AttachmentIndex(); $attachmentIndex->attachment_id = $attachment->primaryKey; $attachmentIndex->entity = get_class($this); $attachmentIndex->entity_id = 0; $attachmentIndex->attribute = "appLogoImage"; $attachmentIndex->save(); return $attachment; }
/** * @param $filePath string * @param * $owner * * @return bool|File * * @throws \Exception * @throws \yii\base\InvalidConfigException */ public function attachFile($file) { $filePath = $file['path']; $attachment = new Attachment(); $attachment->uploadFromFile($filePath); $attachment->name = $file['name']; if ($attachment->save()) { return $attachment; } else { return false; // 存储失败的话的处理 } }
public function saveAvatar($avatar) { if ($this->avatarImage) { $this->avatarImage->delete(); } $attachment = new Attachment(); $attachment->uploadFromFile($avatar); $attachment->save(); $attachmentIndex = new AttachmentIndex(); $attachmentIndex->attachment_id = $attachment->primaryKey; $attachmentIndex->entity = get_class($this); $attachmentIndex->entity_id = $this->primaryKey; $attachmentIndex->attribute = "avatarImage"; $attachmentIndex->save(); }