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;
}
Comment