Hello,
I have a custom entity, which has a file field. The file, I want here is an attachment in documents. The record in the custom entity has one particular file and so I created a 1:1 relationship.
Manually this works, but I do not get to link the file automatically, when uploaded. The record in the custom entity already exists and after that I will upload the file into documents.
I tried with a formula record\relate(), but this did not work.
The documents and the target entity have one common field value. documents = name, and in the custom entity I created a field, which reads the fileName, which then gets the same value as the name field in documents.
Entities: Document, CFoto
common fields: name, fotoDateiname
Link: cDatensatzFoto (1:1 relationship left from Document)
I the tried my luck (because I am no developer) with a hook from documents to the custom entity. Here my code for that:
There is no error, but no result as well. Is there anything wrong in the hook?
I have a custom entity, which has a file field. The file, I want here is an attachment in documents. The record in the custom entity has one particular file and so I created a 1:1 relationship.
Manually this works, but I do not get to link the file automatically, when uploaded. The record in the custom entity already exists and after that I will upload the file into documents.
I tried with a formula record\relate(), but this did not work.
The documents and the target entity have one common field value. documents = name, and in the custom entity I created a field, which reads the fileName, which then gets the same value as the name field in documents.
Entities: Document, CFoto
common fields: name, fotoDateiname
Link: cDatensatzFoto (1:1 relationship left from Document)
I the tried my luck (because I am no developer) with a hook from documents to the custom entity. Here my code for that:
Code:
<?php namespace Espo\Custom\Hooks\Document; use Espo\ORM\Entity; use Espo\Core\Container; use Espo\Core\Exceptions\Error; class LinkFotoHook { private $entityManager; public function __construct(Container $container) { $this->entityManager = $container->get('entityManager'); } /** * @param array<string, mixed> $options */ public function afterSave(Entity $entity, array $options): void { if (array_key_exists('skipHooks', $options) && $options['skipHooks']) { return; } $documentName = $entity->get('name'); $linkName = 'cDatensatzFoto'; if ($documentName) { $foto = $this->entityManager->getRepository('CFoto')->findOne([ 'where' => [ 'fotoDateiname' => $documentName ] ]); if ($foto) { $entity->set($linkName, $foto); $this->entityManager->saveEntity($entity, ['setForceUpdate' => true, 'skipHooks' => true]); } } } }
Comment