Announcement

Collapse
No announcement yet.

How to relate Document and Attachment?

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

  • How to relate Document and Attachment?

    I have a file on the Server,that i want to create a document for. I can create the attachment and the document,but i can not figure out how to relate them.
    PHP Code:
     private function createDocument($filepath,$filename,$folder)
        {
            
    $entityManager $this->getEntityManager();
            
    $attachment $this->createAttachment($filepath,$filename);
            
    $document $entityManager->getEntity("Document");
            
    $document->set("name",$filename);
            
    $entityManager->saveEntity($document);

            
    $entityManager->getRepository("DocumentFolder")->relate($folder,"documents",$document);
           
    //Thats is the line i have problems with
            
    $entityManager->getRepository("Document")->relate($document,"file",$attachment);

            return 
    $document;
        }

    private function 
    createAttachment($filepath,$filename)
        {
            
    $entityManager $this->getEntityManager();
            
    $pdf_content file_get_contents($filepath);
            
    $attachment $entityManager->getEntity('Attachment');

            
    $attachment->set([
                
    'name' => $filename,
                
    'type' => 'application/pdf',
                
    'role' => 'Attachment',
                
    'contents' => $pdf_content,
                
    'relatedType'=> 'Document',
                
    'field' => 'file'
            
    ]);
            
    $entityManager->saveEntity($attachment);
            return 
    $attachment;
        } 

  • #2
    I have now bypassed the problem,by updating the ids in the database
    PHP Code:
        $pdo $this->getEntityManager()->getPDO();
        
    $sth $pdo->prepare($sql =  "UPDATE document SET file_id = '{$attachment->get("id")}' WHERE id = '{$document->get("id")}'");
        
    $sth->execute();
        
    $sth $pdo->prepare($sql =  "UPDATE attachment SET related_id = '{$document->get("id")}' WHERE id = '{$attachment->get("id")}'");
        
    $sth->execute(); 
    But im still intressted in the correct way.
    Last edited by Andorxor; 09-04-2019, 12:21 PM.

    Comment


    • #3
      If anyone else needs the answer, look here: https://forum.espocrm.com/forum/deve...d-a-local-file

      Comment

      Working...
      X