예제 #1
0
 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();
 }
예제 #2
0
 public function afterDelete()
 {
     // 删除关系表
     AttachmentIndex::deleteAll(['attachment_id' => $this->primaryKey]);
     try {
         $media = MediaItem::createFromAttachment($this);
         if ($media->isImage()) {
             $this->deleteThumbs();
         }
         $this->deleteFile();
     } catch (\Exception $ex) {
     }
 }
 public function saveIndex($id)
 {
     $relation = new AttachmentIndex();
     $relation->entity = $this->getEntityClass();
     $relation->entity_id = $this->getEntityId();
     $relation->attribute = $this->attribute;
     $relation->attachment_id = $id;
     $relation->save();
 }
예제 #4
0
 public function getLogoUrl()
 {
     $attachment = Attachment::find()->where([AttachmentIndex::tableName() . '.entity' => get_class($this), AttachmentIndex::tableName() . '.entity_id' => 0, AttachmentIndex::tableName() . '.attribute' => "appLogoImage"])->leftJoin(AttachmentIndex::tableName(), AttachmentIndex::tableName() . '.attachment_id =' . Attachment::tableName() . ".attachment_id")->one();
     if ($attachment) {
         return $attachment->getUrl();
     }
     return null;
 }
예제 #5
0
 public function getAttachmentIndex()
 {
     return $this->owner->hasMany(AttachmentIndex::className(), ['entity_id' => $this->owner->primaryKey()[0]])->where(["entity" => $this->getEntityClass(), "attribute" => $this->attribute]);
 }