Help with a hook needed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shalmaxb
    Senior Member
    • Mar 2015
    • 1724

    #1

    Help with a hook needed

    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:

    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]);
    }
    }
    }
    }
    There is no error, but no result as well. Is there anything wrong in the hook?
  • yuri
    EspoCRM product developer
    • Mar 2014
    • 9299

    #2
    Fixed. Not tested.

    Code:
    <?php
    namespace Espo\Custom\Hooks\Document;
    
    use Espo\ORM\Entity;
    use Espo\ORM\EntityManager;
    
    class LinkFotoHook
    {
        public function __construct(private EntityManager $entityManager)
        {}
    
        public function afterSave(Entity $entity, array $options): void
        {
            if (array_key_exists('skipHooks', $options) && $options['skipHooks']) {
                return;
            }
    
            $documentName = $entity->get('name');
    
            if (!$documentName) {
                return;
            }
    
            $foto = $this->entityManager
                ->getRDBRepository('CFoto')
                ->where(['fotoDateiname' => $documentName])
                ->findOne();
    
            if (!$foto) {
                return;
            }
    
            $linkName = 'cDatensatzFoto';
    
            $entity->set($linkName . 'Id', $foto->getId());
    
            $this->entityManager->saveEntity($entity, ['skipHooks' => true]);
        }
    }
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • shalmaxb
      Senior Member
      • Mar 2015
      • 1724

      #3
      @yuri,
      thank you very much for your help.

      tested right now:
      - on creating a new document, nothing happens, no link is created in neither entity
      - when I change a value from one field (e.g. expiration date) in the recently created document, the record id (not the name) of the linked entity appears.
      - the linked document does not appear in the linked entity
      - when I execute a browser refresh, the link in documents is gone.

      Comment

      • yuri
        EspoCRM product developer
        • Mar 2014
        • 9299

        #4
        Maybe this version will work. Changed to the before save hook.

        Code:
        <?php
        namespace Espo\Custom\Hooks\Document;
        
        use Espo\ORM\Entity;
        
        class LinkFotoHook
        {
            public function __construct()
            {}
        
            public function beforeSave(Entity $entity, array $options): void
            {
                $documentName = $entity->get('name');
        
                if (!$documentName) {
                    return;
                }
        
                $foto = $this->entityManager
                    ->getRDBRepository('CFoto')
                    ->where(['fotoDateiname' => $documentName])
                    ->findOne();
        
                if (!$foto) {
                    return;
                }
        
                $linkName = 'cDatensatzFoto';
        
                $entity->set($linkName . 'Id', $foto->getId());
            }
        }
        If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

        Comment

        Working...