Bonjour la communauté,

Je calle sur l'upload de fichier, je n'y arrive vraiment pas, je perds des journées entières à chercher, à essayer, mais au final, aucun résultat.
Serait-il possible de me donner un coup de main ( pour quelque chose qui me semble assez simple pourtant ).

Je travaille sur 2 entités : réalisations & images.
Les images sont bien sûr liées aux réalisations par une relation ManyToOne.

Mon entité "image"
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
 
namespace App\Entity;
 
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
 
/**
 * @ORM\Entity(repositoryClass="App\Repository\ImageRepository")
 */
class Image
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;
 
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Realisation", inversedBy="images")
     * @ORM\JoinColumn(nullable=false)
     */
    private $realisation;
 
    /**
     * @ORM\Column(type="string", length=255)
     */
    private $imageFile;
 
 
    public function getId(): ?int
    {
        return $this->id;
    }
 
    public function getRealisation(): ?Realisation
    {
        return $this->realisation;
    }
 
    public function setRealisation(?Realisation $realisation): self
    {
        $this->realisation = $realisation;
 
        return $this;
    }
 
    public function getImageFile(): ?string
    {
        return $this->imageFile;
    }
 
    public function setImageFile(string $imageFile): self
    {
        $this->imageFile = $imageFile;
 
        return $this;
    }
 
 
}
Mon entité "Réalisation"
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
 
namespace App\Entity;
 
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
 
/**
 * @ORM\Entity(repositoryClass="App\Repository\RealisationRepository")
 */
class Realisation
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;
 
    /**
     * @ORM\Column(type="string", length=255)
     */
    private $title;
 
    /**
     * @ORM\Column(type="text")
     */
    private $description;
 
    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Image", mappedBy="realisation")
     */
    private $images;
 
    public function __construct()
    {
        $this->images = new ArrayCollection();
    }
 
 
    public function getId(): ?int
    {
        return $this->id;
    }
 
    public function getTitle(): ?string
    {
        return $this->title;
    }
 
    public function setTitle(string $title): self
    {
        $this->title = $title;
 
        return $this;
    }
 
    public function getDescription(): ?string
    {
        return $this->description;
    }
 
    public function setDescription(string $description): self
    {
        $this->description = $description;
 
        return $this;
    }
 
 
    /**
     * @return Collection|Image[]
     */
    public function getImages(): Collection
    {
        return $this->images;
    }
 
    public function addImage(Image $image): self
    {
        if (!$this->images->contains($image)) {
            $this->images[] = $image;
            $image->setRealisation($this);
        }
 
        return $this;
    }
 
    public function removeImage(Image $image): self
    {
        if ($this->images->contains($image)) {
            $this->images->removeElement($image);
            // set the owning side to null (unless already changed)
            if ($image->getRealisation() === $this) {
                $image->setRealisation(null);
            }
        }
 
        return $this;
    }
}
Mon form "ImageType"
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
 
namespace App\Form;
 
use App\Entity\Image;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
 
class ImageType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('image', FileType::class, array(
                'label' => 'Votre image',
                'mapped' => false,
 
            ))
 
        ;
    }
 
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Image::class,
        ]);
    }
}
Mon form "RealisationType" :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
 
namespace App\Form;
 
use App\Entity\Image;
use App\Entity\Realisation;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
 
class RealisationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', TextType::class, array(
                'label' => ' ',
                'attr' => array(
                    'class' => 'form-group',
                    'placeholder' => 'Titre',
                ),
            ))
            ->add('description', TextareaType::class, array(
                'label' => ' ',
                'attr' => array(
                    'placeholder' => 'Description'
                )
            ))
            ->add('image_realisation', ImageType::class, array(
                'label' => 'Votre image',
                'mapped' => false,
            ))
            ->add('submit', SubmitType::class)
        ;
    }
 
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Realisation::class,
        ]);
    }
}
Et mon controller :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
 
namespace App\Controller;
 
use App\Entity\Image;
use App\Entity\Realisation;
use App\Form\RealisationType;
use App\Repository\RealisationRepository;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityManagerInterface;
use Gedmo\Sluggable\Util\Urlizer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Routing\Annotation\Route;
 
class RealisationController extends AbstractController
{
    /**
     * @Route("/realisation", name="realisation")
     */
    public function index(RealisationRepository $repo)
    {
        $realisations = $repo->findAll();
 
        return $this->render('realisation/index.html.twig', [
            'realisations' => $realisations
        ]);
    }
 
    /**
     * Ajout de réalisation
     * @Route("realisation/new", name="realisation_new")
     *
     * @return Response
     */
    public function create(Request $request, EntityManagerInterface $manager){
        $realisation = new Realisation();
        $image = new Image();
 
        $form = $this->createForm(RealisationType::class, $realisation);
        $form->handleRequest($request);
 
        if($form->isSubmitted() && $form->isValid()){
 
          /** @var UploadedFile $uploadedFile */
          $uploadedFile = $form['image_realisation']->getData();
 
          if($uploadedFile){
               $destination = $this->getParameter('kernel.project_dir').'/public/uploads/';
 
               $originalFilename = pathinfo($uploadedFile->getClientOriginalName(), PATHINFO_FILENAME);
 
               $newFilename= Urlizer::urlize($originalFilename).'-'.uniqid().'.'.$uploadedFile->guessExtension();
 
               $uploadedFile->move(
                   $destination,
                   $newFilename
               );
               $image->setImageFile($newFilename);
               $realisation->addImage($image);
          }
 
 
          $manager->persist($realisation);
          $manager->flush();
 
        }
 
        return $this->render('realisation/new.html.twig',[
            'form' => $form->createView()
        ]);
 
    }
 
 
}
Ma page d'affichage
Code twig : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
{% extends 'base.html.twig' %}
 
{% block title %} Création de votre demande{% endblock %}
 
{% block body %}
 
 
    {{ form_start(form) }}
        {{ form_row(form.title) }}
        {{ form_row(form.description) }}
        {{ form_row(form.image_realisation) }}
    {{ form_end(form) }}
{% endblock %}


A force de chipoter, j'ai maintenant cette erreur :
Attempted to call an undefined method named "getClientOriginalName" of class "App\Entity\Image"
Mais Je n'y arrivais déjà pas avant ce message.

Merci beaucoup à vous qui prenez la peine de m'aider, bon dimanche.