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
LowiczakPL
post
Post #2





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

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


1. Uważam że pole Hidden jest zbędne.
Dodajesz zdjęcia za pomocą AJAXa więc od razu powinno być przypisanie do Produktu.

2. Pole ze zdjęciami jest Kolekcją więc transformer powinien zmieniać string na kolekcję i odwrotnie, \
nie obchodzi cię nic więcej bo Symfony zajmie się zapisem .

3. Ale jeśli chcesz na siłe mieć to pole w tej formie to przykładowo w kontrolerze/menadżerze możesz wyciągnąć dane z pola zdjęcie, tak jak napisałeś explode i przypisać pliki do produktu np.

  1. foreach ($pliki as $plik){
  2. $product->getDocuments()->add($plik);
  3. }
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: 18.10.2025 - 09:14