问题描述
我需要有关如何在symfony中上传pdf文件的帮助.通常,学生与pdf卡之间的关系如下:一个学生可以拥有多个pdf卡,一个学生可以拥有一个卡.实体表如下:
I need a help on how to upload a pdf file in symfony. In general, the relationship between the student and the pdf card is as follows: A single student can have several pdf cards and one card for a single student. The entity sheet is as follows:
class FichePDF
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="Nom", type="string", length=255)
*/
private $nom;
/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank(message="Please, upload the evaluation file as a PDF file.")
* @Assert\File(mimeTypes={ "application/pdf" })
*/
private $file;
/**
* @var string
*
* @ORM\Column(name="Path", type="string", length=255)
*/
private $path;
/**
* @ORM\ManyToOne(targetEntity="Polytech\SkillsBundle\Entity\Utilisateur", inversedBy="fichesPdf")
* @ORM\JoinColumn(nullable=false)
*
*/
private $etudiant;
/**
* @ORM\OneToOne(targetEntity="Polytech\SkillsBundle\Entity\SousOccasion")
* @ORM\JoinColumn(name="ssocc_id", referencedColumnName="id")
*/
private $ssocc;
当然有吸气剂和吸气剂.对于学生实体,我添加了这一行
With getters and setters of course. For the student entity I added this line
/**
* @ORM\OneToMany(targetEntity="Polytech\SkillsBundle\Entity\FichePDF" , mappedBy="etudiant", cascade={"remove"})
*/
private $fichesPdf;
我有一个表格,可以检索有关我的应用程序中多个实体的信息,例如教学单位,考试和学生,然后检索pdf文件.
I have a form that retrieves information about several entities in my application such as teaching units, exams and students and then retrieves a pdf file.
<?php
namespace Polytech\SkillsBundle\Form\Rapport;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
class FicheOccasionType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('ues', EntityType::class,
array(
'class' => 'Polytech\SkillsBundle\Entity\UE',
'attr' => array('class' => 'browser-default ue'),
'choice_label' => 'nom',
'label' => false,
'required' => false,
'placeholder' => 'Choisissez une UE'
)
)
->add('etudiants', EntityType::class,
array(
'class' => 'Polytech\SkillsBundle\Entity\Utilisateur',
'attr' => array('class' => 'browser-default etudiants'),
'choice_label' => 'nom',
'label' => false,
'required' => false,
'placeholder' => 'Choisissez un utilisateur'
)
)
->add('file', FileType::class, array('label' => 'PDF File'))
->add('submit', HiddenType::class)
->add('export', ButtonType::class, array('label' => 'Exporter'))
->add('import', ButtonType::class, array('label' => 'Import'));
}
public function getName()
{
return 'fiche_occasion';
}
}
如何将文件检索为上载的文件"并将其添加到数据库中.我阅读了文档,但这与我做的不完全一样.你能帮我吗
How can I retrieve the file as Uploaded file and add it to the database. I read the documentation and it's not exactly what I do. Can you please help me
推荐答案
如文档中所述,您已经中途了:
You are halfway there as is described in the Documentation:
https://symfony.com/doc/current/controller/upload_file.html
您已经执行的以下步骤:
The following steps you already did:
- 将该属性添加到您的实体
- 将上载元素添加到表单中
现在,您必须处理上载的文件,并将上载路径添加到实体.现在,在处理表单的控制器中,您必须执行以下操作:
Now you have to handle the uploaded file and add the upload path to the entity. In your controller where you handle the form you now have to do the following:
$fiche = new FichePDF();
$form = $this->createForm(FichePDF::class, $fiche);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$file = $fiche->getFile();
// Generate a unique name for the file before saving it
$fileName = md5(uniqid()).'.'.$file->guessExtension();
// Move the file to the directory where brochures are stored
$file->move(
$this->getParameter('upload_directory'),
$fileName
);
// Update the 'fichePDF' property to store the PDF file name
// instead of its contents
$fiche->setFile($fileName);
// Persist $fichePDF and do whatever else you want to
}
这篇关于上载带有symfony的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!