Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SF][SF3] Data transformer
netvalue
post
Post #1





Grupa: Zarejestrowani
Postów: 199
Pomógł: 2
Dołączył: 9.06.2008

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


Zastanawiam się jak podejść do takiego tematu: mam dwie entity:
Product , ProductDocument z relacją ManyToOne (zdjęcia do produktu).

Uzytkownik dodaje produkt (form builder) w tym formularzu mam odrazu pole typu hidden w którym wrzucam nazwy plików (document) które wgrał przez plugin ajaxowy. Czyli wartość tego pola będzie przykładowo:

  1. "plik1.jpg;plik2.jpg;plik3.jpg"


Potrzebuje z powyższego stringa wyciągnąć nazwę pliku , stworzyć obiekt ProductDocument i powiazać z Product.
W obecniej chwili ten formularz zapisuje mi Product , natomiast ProductDocument tez dodaje się ale bez powiazania z Product, ponadto dodaj mi pełną wartość do nazwy ""plik1.jpg;plik2.jpg;plik3.jpg"" zamiast każdy oddzielnie. Wiem że trzeba użyć explode(';'.$wartosc) ale nie wiem gdzie


w ProduktForm mam
  1. class ProductFormType extends AbstractType
  2. {
  3.  
  4. private $transformer;
  5.  
  6. public function __construct(DocumentTransformer $transformer)
  7. {
  8. $this->transformer = $transformer;
  9. }
  10.  
  11. public function buildForm(FormBuilderInterface $builder, array $options)
  12. {
  13. $builder
  14. ->add('catalogNo', ChoiceType::class, array(
  15. 'attr' => array('class' => 'select2live','required'=>'required'),
  16. 'choices'=>array()
  17. ))
  18. ->add('productDocument', HiddenType::class)
  19. ->add('purchasedAt', DateType::class, [
  20. 'widget' => 'single_text',
  21. 'attr' => ['class' => 'js-datepicker'],
  22. 'html5' => false,
  23. ]);
  24.  
  25. $builder->get('productDocument')->addModelTransformer($this->transformer);
  26.  
  27. }
  28.  
  29. public function configureOptions(OptionsResolver $resolver)
  30. {
  31. $resolver->setDefaults([
  32. 'data_class' => 'AppBundle\Entity\Product'
  33. ]);
  34. }
  35.  
  36. public function getBlockPrefix()
  37. {
  38. return 'app_bundle_product';
  39. }
  40. }


DataTransformer.php
  1.  
  2. class DocumentTransformer implements DataTransformerInterface
  3. {
  4.  
  5. private $em;
  6.  
  7. public function __construct(EntityManagerInterface $em)
  8. {
  9. $this->em = $em;
  10. }
  11.  
  12. public function transform($document)
  13. {
  14. // TODO: Implement transform() method.
  15. if(null == $document)
  16. {
  17. return '';
  18. }
  19. return $document->getId();
  20. }
  21.  
  22. public function reverseTransform($files)
  23. {
  24. if(!$files)
  25. {
  26. return;
  27. }
  28. $document = new ProductDocument();
  29. $document->setName($files);
  30. return $document;
  31. }
  32. }

Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Pilsener
post
Post #2





Grupa: Zarejestrowani
Postów: 1 590
Pomógł: 185
Dołączył: 19.04.2006
Skąd: Gdańsk

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


Wystarczy użyć standardowej kolekcji:
https://symfony.com/doc/current/reference/f...collection.html

Nie trzeba żadnych pól typu hidden czy data transformerów, robisz pole formularza typu "MyFile", jak klikasz "dodaj zdjęcie" to JS tworzy nowe pole na podstawie prototype (jest nawet gotowy JS w dokumentacji)
Można tez użyć standardowego pola typu file, ale własne jest lepsze bo można od razu tam wrzucić np. miniaturkę fotki, dodatkowy checkbox "usuń zdjęcie", jakiś własny event listener itp.
Tego typu rozwiązania (np. dodawanie wielu adresów do jednego usera) są w Symfony dość oklepane - wystarczy dokumentacja i trochę praktyki.

A jak chcesz robić po swojemu to nie mieszaj do tego formularzy Symfony, bo szkoda czasu na tego typu protezy czy workaroundy.
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 12.10.2025 - 10:45