The idea here is to add a file (photo ) to a user or other entity. I decided that this file could have its own entity Media.
As explained in the symfony book , I created the add file function in the entity 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;
}
}
Then, i created the Media FormType with the file upload:
<?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'
));
}
}
And now, i want to embed this form in my user form:
<?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'
));
}
}
But in my new user view, this is the result:
some details: - i am in symfony 2.8 - i used the symfony boot to this part (http://symfony.com/doc/2.8/cookbook/controller/upload_file.html) I know i can use the VichUploaderBundle but i want to learn first.
Thanks in advance for your help, I hope I was clear enough , and my poor English will not create too many understanding problems.
Edit: i try to make MediaType service.
in my Media FormType, i added:
private $mediaService;
public function __construct(MediaService $mediaService)
{
$this->mediaService = $mediaService;
}
(as explained in symfony book)
And in my app/config/services.yml:
admin.form.mediatype:
class: AdminBundle\Form\MediaType
tags:
- { name: form.type }
In my Utilisateur FormType, i add:
->add('media', MediaType::class)
Now, i have the error:
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
Field type "entity"
only allows you to fetch from already existing entities in your database. Right there, you want to use the MediaType form type you defined. To do so:
1) Define your MediaType as a service. For example, using YML:
services:
app.form.media:
class: AdminBundle\Form\MediaType
public: false
tags:
- { name: form.type }
2) Replace
->add('media','entity', array('class'=>'AdminBundle:Media'))
by:
->add('media', MediaType::class)
And this should be a bit better already. Now you can customize the display of your form through form themes (check the documentation for defining custom FormTypes)