Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SF][SF3] Data transformer
netvalue
post 11.04.2018, 09:16:34
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
netvalue
post 17.04.2018, 10:01:30
Post #2





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

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


Dziękuję za pomoc smile.gif jesteś wielki
btw. brakowało mi czegoś takiego smile.gif

  1. public function addProductDocument(ProductDocument $productDocument)
  2. {
  3. $productDocument->setProduct($this);
  4. $this->productDocument->add($productDocument);
  5. }
  6.  
  7.  
  8. public function removeProductDocument(ProductDocument $productDocument)
  9. {
  10. $this->productDocument->removeElement($productDocument);
  11. }

Go to the top of the page
+Quote Post

Posty w temacie


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: 26.04.2024 - 09:03