Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony][SF] Multiupload plików
damianooo
post 23.03.2021, 21:38:42
Post #1





Grupa: Zarejestrowani
Postów: 493
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

Ostrzeżenie: (0%)
-----


Próbuję załadować kilka plików na raz w formularzu.
Niestety mam błąd:

  1. Expected argument of type "App\Entity\Image", "instance of Symfony\Component\HttpFoundation\File\UploadedFile" given at property path "files".

Nie wiem co mam źle.

Błąd wskazuje na kontroler na metodę handleRequest:
Form->handleRequest(object(Request)) in C:\xampp\htdocs\biuro\src\Controller\OfferController.php (line 29)

Tak wyglądają moje klasy:

  1. class OfferType extends AbstractType
  2. {
  3. public function buildForm(FormBuilderInterface $builder, array $options)
  4. {
  5. $builder
  6. ->add('name', TextType::class)
  7. ->add('description', TextareaType::class)
  8. ->add('files',FileType::class, [
  9. 'multiple' => true
  10. ]
  11. )



  1. class OfferController extends AbstractController
  2. {
  3. public function createOffer(Request $request) {
  4.  
  5. $offer = new Offer();
  6. $form = $this->createForm(OfferType::class, $offer);
  7. $form->handleRequest($request);


  1. class Offer
  2. {
  3. /**
  4.   * @ORM\OneToMany(targetEntity="Image", mappedBy="offer", cascade={"persist"})
  5.   */
  6. private $files;



  1. <tr>
  2. <th>Nazwa:</th>
  3. <td>{{ form_widget(form.name) }}</td>
  4. </tr>
  5. <tr>
  6. <th>Opis:</th>
  7. <td>{{ form_widget(form.description) }}</td>
  8. </tr>
  9. <tr>
  10. <th>Zdjęcia:</th>
  11. <td>{{ form_widget(form.files) }}</td>
  12. </tr>
  13. <tr>
  14. <th></th>
  15. <td>{{ form_widget(form.save) }}</td>
  16. </tr>
  17. </tbody>


Ten post edytował damianooo 23.03.2021, 21:40:22
Go to the top of the page
+Quote Post
LowiczakPL
post 25.03.2021, 19:09:55
Post #2





Grupa: Zarejestrowani
Postów: 531
Pomógł: 55
Dołączył: 3.01.2016
Skąd: Łowicz

Ostrzeżenie: (0%)
-----


W Encji powinieneś mieć string lub arrray, Przecież nie zapisujesz plików w bazie tylko na serwerze a w bazie masz zapisaną jedynie nazwę poliku lub plików.


--------------------
Szukam zleceń Symfony, Laravel, Back-End, Front-End, PHP, MySQL ...
Go to the top of the page
+Quote Post
damianooo
post 28.03.2021, 21:16:31
Post #3





Grupa: Zarejestrowani
Postów: 493
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

Ostrzeżenie: (0%)
-----


Rozwiązałem swój problem tak:

Controller:

  1. public function createOffer(Request $request) {
  2.  
  3. $offer = new Offer();
  4. $form = $this->createForm(OfferType::class, $offer);
  5. $form->handleRequest($request);
  6.  
  7. if ($form->isSubmitted() && $form->isValid()) {
  8.  
  9. $offer = $form->getData();
  10.  
  11. $files = $form['files']->getData();
  12.  
  13. $em = $this->getDoctrine()->getManager();
  14.  
  15. if ($files)
  16. {
  17. foreach ($files as $file)
  18. {
  19. $image = new Image();
  20. $image->setOffer($offer);
  21. $image->setSize(25);
  22. $image->setPath($file->getRealPath());
  23. $image->setName($file->getClientOriginalName());
  24.  
  25. $file->move(
  26. $this->getParameter('file_directory'),
  27. $file->getClientOriginalName()
  28. );
  29.  
  30. $em->persist($image);
  31. }
  32. }
  33.  
  34. $em->persist($offer);
  35. $em->flush();



OfferType:


  1. public function buildForm(FormBuilderInterface $builder, array $options)
  2. {
  3. $builder
  4. ->add('name', TextType::class)
  5. ->add('description', TextareaType::class)
  6. ->add('files',FileType::class, [
  7. 'label' => 'Zdjęcia',
  8. 'multiple' => true,
  9. 'mapped' => false,
  10. 'required' => true
  11. ]
  12. )

Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 27.04.2024 - 22:53