Announcement

Collapse
No announcement yet.

Creating a new attachment with the ORM and a local file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating a new attachment with the ORM and a local file

    I'm looking for an example of how to create a new attachment record with a local file. Ideally it would be stored in the default storage mechanism (in this case, AWS, but that isn't important). I've been looking through the core files, but I can't find anything that helps me understand what to do.

    Something like this:
    PHP Code:
    $filePath "/path/to/file/";
    $fileName "test.xlsx";
    $path $this->someService->generatefile($filePath$fileName);

    $attachment someFunction($path); 
    I found this in application/Espo/Services/Email.php:

    PHP Code:
                $source $this->entityManager->getEntityById(Attachment::ENTITY_TYPE$attachmentId);

                if (
    $source) {
                    
    /** @var Attachment $attachment */
                    
    $attachment $this->entityManager->getNewEntity(Attachment::ENTITY_TYPE);

                    
    $attachment->set('role'Attachment::ROLE_ATTACHMENT);
                    
    $attachment->set('type'$source->getType());
                    
    $attachment->set('size'$source->getSize());
                    
    $attachment->set('global'$source->get('global'));
                    
    $attachment->set('name'$source->getName());
                    
    $attachment->set('sourceId'$source->getSourceId());
                    
    $attachment->set('storage'$source->getStorage());

                    if (
    $field) {
                        
    $attachment->set('field'$field);
                    }

                    if (
    $parentType) {
                        
    $attachment->set('parentType'$parentType);
                    }

                    if (
    $parentType && $parentId) {
                        
    $attachment->set('parentId'$parentId);
                    }

                    if (
    $this->fileStorageManager->exists($source)) {
                        
    $this->entityManager->saveEntity($attachment);

                        
    $contents $this->fileStorageManager->getContents($source);

                        
    $this->fileStorageManager->putContents($attachment$contents);

                        
    $ids[] = $attachment->getId();

                        
    $names->{$attachment->getId()} = $attachment->getName();
                    }
                }
    ​ 
    I have not been able to make it work, though, so I'm hoping to find a simpler example.

  • #2
    There may be a better way to do it, but here is what I wrote, which works:
    PHP Code:
        ...
        
    $writer \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($workbook"Xlsx");
        
    $writer->save($fileName);

        
    $attachment $this->entityManager->getNewEntity('Attachment');
        
    $attachment->set('contents'file_get_contents($fileName));
        
    $attachment->set('name'"Claim Spreadsheet {$now}.xlsx");
        
    $attachment->set('type'"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        
    $attachment->set('role'"Attachment");
        
    $this->entityManager->saveEntity($attachment);

        
    $folder $this->entityManager->getRDBRepository("DocumentFolder")
            ->
    where(array("name" => "Claims"))->findOne();

        
    $document $this->entityManager->createEntity("Document", array(
          
    "name" => "{$attachment->get("name")}",
          
    "status" => "Active",
          
    "type" => "Claim",
          
    "fileId" => $attachment->id,
          
    "fileName" => $attachment->get("name"),
          
    "folderId" => $folder->id,
        ));

        
    unlink($fileName);

        foreach(
    $claims as $claim) {
          
    $this->entityManager->getRDBRepository("Document")->getRelation($document"claims")->relate($claim);
        }
    ​ 

    Comment

    Working...
    X