【发布时间】:2016-01-22 13:34:35
【问题描述】:
这里的想法是向用户或其他实体添加文件(照片)。我决定这个文件可以有自己的实体媒体。
正如 symfony book 中解释的那样,我在实体 Media 中创建了添加文件功能:
<?php
namespace AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Media
*
* @ORM\Table(name="Media", indexes={@ORM\Index(name="fk_Media_Cat_Media1_idx", columns={"Cat_Media_ID"})})
* @ORM\Entity
*/
class Media
{
/**
* @var string
*
* @ORM\Column(name="Libelle", type="string", length=45, nullable=true)
*/
private $libelle;
/**
* @var integer
*
* @ORM\Column(name="ID", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \AdminBundle\Entity\CatMedia
*
* @ORM\ManyToOne(targetEntity="AdminBundle\Entity\CatMedia")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="Cat_Media_ID", referencedColumnName="ID")
* })
*/
private $catMedia;
/**
* @ORM\Column(type="string")
*
*
* @Assert\File(mimeTypes={ "application/image" })
*/
private $file;
/**
* Set libelle
*
* @param string $libelle
* @return Media
*/
public function setLibelle($libelle)
{
$this->libelle = $libelle;
return $this;
}
/**
* Get libelle
*
* @return string
*/
public function getLibelle()
{
return $this->libelle;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set catMedia
*
* @param \AdminBundle\Entity\CatMedia $catMedia
* @return Media
*/
public function setCatMedia(\AdminBundle\Entity\CatMedia $catMedia = null)
{
$this->catMedia = $catMedia;
return $this;
}
/**
* Get catMedia
*
* @return \AdminBundle\Entity\CatMedia
*/
public function getCatMedia()
{
return $this->catMedia;
}
public function setfile($file)
{
$this->file = $file;
return $this;
}
public function getFile()
{
return $this->file;
}
}
然后,我通过文件上传创建了 Media FormType:
<?php
namespace AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
class MediaType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('libelle')
->add('catmedia','entity', array('class'=>'AdminBundle:CatMedia',
'property'=>'libelle'))
->add('file', FileType::class, array('label' => 'Image (Png or jpg file)'))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AdminBundle\Entity\Media'
));
}
}
现在,我想将此表单嵌入到我的用户表单中:
<?php
namespace AdminBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UtilisateurType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nom')
->add('prenom')
->add('societe')
->add('mail')
->add('tel')
->add('password')
->add('catutilisateur','entity', array('class'=>'AdminBundle:CatUtilisateur',
'property'=>'libelle'))
->add('media','entity', array('class'=>'AdminBundle:Media'))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AdminBundle\Entity\Utilisateur'
));
}
}
但在我的新用户视图中,结果如下:
一些细节: - 我在 symfony 2.8 - 我使用 symfony 引导到这部分 (http://symfony.com/doc/2.8/cookbook/controller/upload_file.html) 我知道我可以使用 VichUploaderBundle 但我想先学习。
在此先感谢您的帮助,我希望我说的足够清楚,我糟糕的英语不会造成太多的理解问题。
编辑:我尝试制作 MediaType 服务。
在我的 Media FormType 中,我添加了:
private $mediaService;
public function __construct(MediaService $mediaService)
{
$this->mediaService = $mediaService;
}
(如 symfony 书中所述)
在我的 app/config/services.yml 中:
admin.form.mediatype:
class: AdminBundle\Form\MediaType
tags:
- { name: form.type }
在我的 Utilisateur FormType 中,我添加:
->add('media', MediaType::class)
现在,我有错误:
Catchable Fatal Error: Argument 1 passed to AdminBundle\Form\MediaType::__construct() must be an instance of AdminBundle\Form\MediaService, none given, called in /var/www/erdf/app/cache/dev/appDevDebugProjectContainer.php on line 294 and defined
【问题讨论】: