Php 使用symfony上载PDF文件
我需要一个关于如何上传在symfony pdf文件的帮助。一般来说,学生与pdf卡之间的关系如下:一个学生可以有多张pdf卡,一个学生可以有一张pdf卡。实体表如下所示:Php 使用symfony上载PDF文件,php,symfony,pdf,Php,Symfony,Pdf,我需要一个关于如何上传在symfony pdf文件的帮助。一般来说,学生与pdf卡之间的关系如下:一个学生可以有多张pdf卡,一个学生可以有一张pdf卡。实体表如下所示: class FichePDF { /** * @var int * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */
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;
当然是有接球手和二传手。对于学生实体,我添加了这一行
/**
* @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';
}
}
如文档中所述,您已经完成了一半:
您已经执行了以下步骤:
将属性添加到实体中
将upload元素添加到表单中
现在,您必须处理上载的文件并将上载路径添加到实体。在处理表单的控制器中,现在必须执行以下操作:
$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
}
此代码$fiche=new FichePDF()$form=$this->createForm(FichePDF::class,$fiche);返回此错误:属性“ues”或方法“getUes()”、“ues()”、“isUes()”、“hasUes()”、“\uu get()”中的任何一个都不存在并且在类中具有公共访问权限“Polytech\SkillsBundle\Entity\FichePDF。因为表单返回ues实体的id…啊,您的表单没有绑定到实体。在这种情况下,您可以省略实体,而不是$file=$fiche->getFile()
您必须执行类似$form->get('file')->getData()
的操作来获取文件名。或者,您可以更仔细地跟踪文档并为该实体创建一个表单(请参阅我答案中的链接)。谢谢您的帮助!