Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony][Symfony2] UpdateAction przy uploadzie pliku
mattix19
post
Post #1





Grupa: Zarejestrowani
Postów: 32
Pomógł: 0
Dołączył: 11.07.2010

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


Witam,

Mam taką oto akcję update do bazy danych:
  1. public function updateAction(Request $request, $id)
  2. {
  3. $em = $this->getDoctrine()->getManager();
  4.  
  5. $entity = $em->getRepository('(...)Bundle:Products')->find($id);
  6.  
  7. if (!$entity) {
  8. throw $this->createNotFoundException('Unable to find Products entity.');
  9. }
  10.  
  11. $editForm = $this->createEditForm($entity);
  12. $editForm->handleRequest($request);
  13.  
  14. if ($editForm->isValid()) {
  15. $file = $editForm->get('photo')->getData();
  16. $name = $file->getClientOriginalName();
  17. $file->move('../web/bundles/static/img/', $name);
  18.  
  19. $product = new Products();
  20. $product->setName( $editForm->get('name'));
  21. $product->setPhoto($name);
  22. $product->setEAN( $editForm->get('eAN'));
  23. $product->setFeatures( $editForm['features']->getData());
  24.  
  25. $em->persist($product);
  26. $em->flush();
  27.  
  28. return $this->redirect($this->generateUrl('admin_produkty_edit', array('id' => $id)));
  29. }
  30.  
  31. return $this->render('(..)Bundle:Products:edit.html.twig', array(
  32. 'entity' => $entity,
  33. 'edit_form' => $editForm->createView(),
  34. ));
  35. }

Problem polega na tym jak wykonać update tej funkcji:
  1. $product->setFeatures();
?
formularz wygląda tak:
  1. private function createEditForm($entity)
  2. {
  3. $form = $this->createFormBuilder($entity)
  4. ->add('name', 'text', array('label' => 'Nazwa'))
  5. ->add('photo', 'file', array('label' => 'Zdjęcie', 'data' => ''))
  6. ->add('eAN', 'text', array('label' => 'Kod EAN'))
  7. ->add('features' , 'entity', array(
  8. 'class' => '(...)Bundle:Features',
  9. 'property' => 'name',
  10. 'multiple' => true,
  11. "expanded" => true,
  12. 'label' => 'Wybierz cechy: ',
  13. ))
  14. ->add('submit', 'submit', array('label' => 'Zapisz zmiany'))
  15. ->getForm();
  16.  
  17. return $form;
  18. }


a akcja dodawania do bazy tak:

  1. public function createAction(Request $request)
  2. {
  3. $form = $this->createCreateForm();
  4. $form->submit($request);
  5. if ($form->isValid()) {
  6. $data = $form->getData();
  7. $name = $data['photo']->getClientOriginalName(); //nazwa pliku
  8. $data['photo']->move('../web/bundles/static/img/', $name);
  9.  
  10. $em = $this->getDoctrine()->getManager();
  11. $product = new Products();
  12. $product->setName($data['name']);
  13. $product->setPhoto($name);
  14. $product->setEAN($data['eAN']);
  15. $product->setFeatures($data['features']);
  16. $em->persist($product);
  17. $em->flush();
  18.  
  19. return $this->redirect($this->generateUrl('produkty'));
  20. }
  21.  
  22. return $this->render('(...)Bundle:Products:new.html.twig', array(
  23. 'form' => $form->createView(),
  24. ));
  25. }

w przypadku dodawania do bazy wszystko działa wystarczy że wrzuciłem tablicę do funckji setFeatures() natomiast jak wykonuję update to ta funkcja nie działa. Jak wykonać update do bazy która ma relację m:n i jednocześnie wykonać upload pliku?
pozdrawiam

Ten post edytował mattix19 19.09.2013, 21:21:58
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: 26.09.2025 - 20:22