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:
I found this in application/Espo/Services/Email.php:
I have not been able to make it work, though, so I'm hoping to find a simpler example.
Something like this:
PHP Code:
$filePath = "/path/to/file/";
$fileName = "test.xlsx";
$path = $this->someService->generatefile($filePath, $fileName);
$attachment = someFunction($path);
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();
}
}
Comment