我需要有关如何在 symfony 中上传 pdf 文件的帮助。一般来说,学生和pdf卡之间的关系如下:单个学生可以拥有多张pdf卡,单个学生可以拥有一张卡。实体表如下:
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;
当然还有 getter 和 setter。对于学生实体,我添加了这一行
/**
* @ORM\OneToMany(targetEntity="Polytech\SkillsBundle\Entity\FichePDF" , mappedBy="etudiant", cascade={"remove"})
*/
private $fichesPdf;
我有一个表单,可以检索有关我的应用程序中的多个实体(例如教学单位、考试和学生)的信息,然后检索 pdf 文件。
<?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';
}
}
如何检索文件作为上传文件并将其添加到数据库中。我阅读了文档,但这并不完全是我所做的。你能帮我吗
最佳答案
正如文档中所述,您已经完成了一半:
https://symfony.com/doc/current/controller/upload_file.html
您已执行以下步骤:
- 将属性添加到您的实体
- 将上传元素添加到表单
现在您必须处理上传的文件并将上传路径添加到实体。在处理表单的 Controller 中,您现在必须执行以下操作:
$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
}
关于php - 使用 symfony 上传 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43001978/