Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony2][Symfony] file upload nie podoba mi się
Foxx
post
Post #1





Grupa: Zarejestrowani
Postów: 896
Pomógł: 76
Dołączył: 15.11.2003
Skąd: Sosnowiec/Kraków

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


Właśnie po raz pierwszy zaimplementowałem upload obrazka + Doctrine i mam pewne wątpliwości. Chodzi o nakład pracy w kodzie potrzebny do przeprowadzenia tej operacji. Zrobiłem to w oparciu o lifecycle callbacks i musiały powstać 3 pola i 11 metod w obiekcie. Fajnie, że nie trzeba nic robić w kontrolerze i rozumiem, że część tych metod to rzeczy w stylu getWebPath i jak mi się nie podoba to nie muszę koniecznie ich używać, ale mimo wszystko to wydaje mi się trochę dziwne. Strach pomyśleć co się stanie jak będę miał 6 plików przy obiekcie.

A na koniec jeszcze czytam w cookbook, że "The PreUpdate and PostUpdate callbacks are only triggered if there is a change in one of the entity's field that are persisted" więc jak zmienię tylko awatar w profilu usera to on mi się nie zapisze :/ Trochę to załamujące, czy to podejście z cookbook jest naprawdę optymalne?

Ten post edytował Foxx 17.04.2013, 13:48:09
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Crea17
post
Post #2





Grupa: Zarejestrowani
Postów: 21
Pomógł: 0
Dołączył: 4.05.2010

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


Witam.

Próbuję zrobić uploader wg tego co napisał Crozin, jednak po drodze napotkałem problem. Jestem początkujący w Symfony, stąd też mój problem.

Wydaje mi się, że zgubiłem się na tworzeniu listenera.

Serwis:
  1. <service id="my_user.imageupload" class="My\UserBundle\Form\Domain\ImageUpload">
  2. <tag name="kernel.event_listener" event="kernel.exception" method="onKernelException"/>
  3. </service>


Listener:
  1. <?php
  2.  
  3. namespace My\UserBundle\EventListener;
  4.  
  5. class ImageUploadListener
  6. {
  7.  
  8. public function __construct()
  9. {
  10. }
  11.  
  12. public function postBind(FormEvent $event) {
  13. $data = $event->getData();
  14.  
  15. if (!$data instanceof ImageUpload) {
  16. return;
  17. }
  18.  
  19. if ($data->getImage()->delete()) {
  20. // usuwamy plik i aktualizujemy nasz obiekt
  21. PropertyAccess::getPropertyAccessor()->setValue($data->getObject(), null);
  22.  
  23. return;
  24. }
  25.  
  26. // Tutaj wykonujemy zapis pliku w odpowiednym miejscu, a korzystając z ProeprtyAccess ustawiamy ścieżkę do pliku:
  27. PropertyAccess::getPropertyAccessor()->setValue($data->getObject(), $data->getImagePath());
  28. }
  29. }


Kontroler:
  1. $user = $this->container->get('security.context')->getToken()->getUser();
  2.  
  3. $data = new ImageUpload($user, new Image(), 'object.avatar');
  4.  
  5. $form = $this->createForm(new ProfileMainFormType(), $data);
  6.  
  7. $form->handleRequest($request);
  8.  
  9. if ($form->isValid()) {
  10.  
  11. var_dump($form->getData()->getObject());
  12.  
  13. echo 'ok form 1';
  14. }


Błąd jaki otrzymuje:
Kod
ContextErrorException: Warning: Missing argument 1 for My\UserBundle\Form\Domain\ImageUpload::__construct(), called in W:\wamp\www\symfony\app\cache\dev\appDevDebugProjectContainer.php on line 2930 and defined in W:\wamp\www\symfony\src\My\UserBundle\Form\Domain\ImageUpload.php line 14


Czy może mnie ktoś nakierować co robię źle?

*********************************************
Edit:

Powoli wraz z dokumentacją Symfony pokonuje trudności napotkane po drodze. Jednak jednego problemu nie mogę pokonać.

Kiedy uploaduje plik, otrzymuje notice
Kod
ContextErrorException: Runtime Notice: Only variables should be passed by reference in W:\wamp\www\symfony\src\My\UserBundle\Form\Type\ProfileAvatarFormType.php line 37


//ProfileAvatarFormType
  1. <?php
  2.  
  3. namespace My\UserBundle\Form\Type;
  4.  
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\Form\FormEvents;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\PropertyAccess\PropertyAccess;
  10. use My\UserBundle\Form\Domain\ImageUpload;
  11.  
  12.  
  13. class ProfileAvatarFormType extends AbstractType
  14. {
  15. public function buildForm(FormBuilderInterface $builder, array $options)
  16. {
  17. $builder
  18. ->add('avatar', 'file', array('mapped' => false))
  19. ->add('save', 'submit');
  20.  
  21. $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
  22.  
  23. $data = $event->getData();
  24.  
  25. if (!$data instanceof ImageUpload) {
  26. return;
  27. }
  28.  
  29. if ($data->getImage()->getDelete()) {
  30. // usuwamy plik i aktualizujemy nasz obiekt
  31. PropertyAccess::getPropertyAccessor()->setValue($data->getObject(), null);
  32.  
  33. return;
  34. }
  35.  
  36. //Tutaj wykonujemy zapis pliku w odpowiednym miejscu, a korzystając z ProeprtyAccess ustawiamy ścieżkę do pliku:
  37. PropertyAccess::getPropertyAccessor()->setValue($data->getObject(), $data->getImagePath());
  38. });
  39. }
  40.  
  41. public function getName()
  42. {
  43. return 'my_user_profileavatar';
  44. }
  45. }


//Kontroler
  1. public function indexAction(Request $request)
  2. {
  3. $user = $this->container->get('security.context')->getToken()->getUser();
  4.  
  5. $data = new ImageUpload($user, new Image(), 'avatar');
  6.  
  7. $form = $this->createForm(new ProfileAvatarFormType(), $data);
  8.  
  9.  
  10. $form->handleRequest($request);
  11.  
  12. if ($form->isValid()) {
  13. $user = $this->container->get('security.context')->getToken()->getUser();
  14. $data = $form->getData();
  15. $userManager = $this->container->get('fos_user.user_manager');
  16. $user->setAvatar($data->getAvatar());
  17.  
  18. $userManager->updateUser($user);
  19.  
  20. echo 'ok form';
  21. }
  22.  
  23.  
  24. return array('formMain' => $form->createView());
  25. }


//ImageUpload
  1. namespace My\UserBundle\Form\Domain;
  2.  
  3. use Symfony\Component\Validator\Constraints as Assert;
  4.  
  5. class ImageUpload {
  6. /** @Assert\Valid(deep=true) */
  7. private $object;
  8.  
  9. /** @Assert\Valid(deep=true) */
  10. private $image;
  11.  
  12. private $imagePath;
  13.  
  14. public function __construct($object, Image $image, $imagePath) {
  15. $this->object = $object;
  16. $this->image = $image;
  17. $this->imagePath = $imagePath;
  18. }
  19.  
  20. /**
  21.   * @param mixed $image
  22.   */
  23. public function setImage($image)
  24. {
  25. $this->image = $image;
  26. }
  27.  
  28. /**
  29.   * @return mixed
  30.   */
  31. public function getImage()
  32. {
  33. return $this->image;
  34. }
  35.  
  36. /**
  37.   * @param mixed $imagePath
  38.   */
  39. public function setImagePath($imagePath)
  40. {
  41. $this->imagePath = $imagePath;
  42. }
  43.  
  44. /**
  45.   * @return mixed
  46.   */
  47. public function getImagePath()
  48. {
  49. return $this->imagePath;
  50. }
  51.  
  52. /**
  53.   * @param mixed $object
  54.   */
  55. public function setObject($object)
  56. {
  57. $this->object = $object;
  58. }
  59.  
  60. /**
  61.   * @return mixed
  62.   */
  63. public function getObject()
  64. {
  65. return $this->object;
  66. }
  67.  
  68. }


//Image
  1. <?php
  2.  
  3. namespace My\UserBundle\Form\Domain;
  4.  
  5. use Symfony\Component\Validator\Constraints as Assert;
  6.  
  7. class Image {
  8. /**
  9.   * @Assert\NotBlank
  10.   * @Assert\Image(minWidth=50, groups={"UserAvatar"})
  11.   * @Assert\Image(minWidth=500, maxWidth=5000, groups={"UserProfile"})
  12.   */
  13. private $file;
  14.  
  15. /**
  16.   * @Assert\Type("boolean")
  17.   */
  18. private $delete = false;
  19.  
  20. /**
  21.   * @param mixed $delete
  22.   */
  23. public function setDelete($delete)
  24. {
  25. $this->delete = $delete;
  26. }
  27.  
  28. /**
  29.   * @return mixed
  30.   */
  31. public function getDelete()
  32. {
  33. return $this->delete;
  34. }
  35.  
  36. /**
  37.   * @param mixed $file
  38.   */
  39. public function setFile($file)
  40. {
  41. $this->file = $file;
  42. }
  43.  
  44. /**
  45.   * @return mixed
  46.   */
  47. public function getFile()
  48. {
  49. return $this->file;
  50. }
  51.  
  52. }


W PropertyAccessor metoda setValue posiada 3 parametry, jednak Crozin podał tylko 2 (w poprzedniej wersji symfony były 2 parametry?)
  1. public function setValue(&$objectOrArray, $propertyPath, $value)


Wracając do błędu, syfony pokazuje notice gdy przekazuje obiekt w parametrze $objectOrArray, tylko dlaczego?
Kolejna sprawa to jaki powinien być trzeci argument setValue()?

Ten post edytował Crea17 19.02.2014, 23:28:40
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: 15.10.2025 - 04:07