Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php+symfony] Error: Call to a member function move() on a non-object
dopelganger
post 23.09.2015, 13:34:08
Post #1





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


cześć
dodałem do profilu użytkownika możliwość upload obrazka (avatar), no i pięknie działa. Ale po zalogowaniu się do konta a następnie przekierowaniu -> wywala bład:

Error: Call to a member function move() on a non-object

czepia się o linię którą wyszczałkowałem smile.gif Prosze o pomoc.


Kod klasy User.php

  1.  
  2. public function getFullImagePath() {
  3. return null === $this->image ? null : $this->getUploadRootDir(). $this->image;
  4. }
  5.  
  6. protected function getUploadRootDir() {
  7. // the absolute directory path where uploaded documents should be saved
  8. return $this->getTmpUploadRootDir().$this->getId()."/";
  9. }
  10.  
  11. protected function getTmpUploadRootDir() {
  12. // the absolute directory path where uploaded documents should be saved
  13. return __DIR__ . '/../../../../web/images/uploads/';
  14. }
  15.  
  16. /**
  17.   * @ORM\PrePersist()
  18.   * @ORM\PreUpdate()
  19.   */
  20. public function uploadImage() {
  21. // the file property can be empty if the field is not required
  22. if (null === $this->image) {
  23. return;
  24. }
  25. if(!$this->id){
  26. $this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
  27. }else{
  28. $this->image->move($this->getUploadRootDir(), $this->image->getClientOriginalName()); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  29. }
  30. $this->setImage($this->image->getClientOriginalName());
  31. }
  32.  
  33. /**
  34.   * @ORM\PostPersist()
  35.   */
  36. public function moveImage()
  37. {
  38. if (null === $this->image) {
  39. return;
  40. }
  41. if(!is_dir($this->getUploadRootDir())){
  42. mkdir($this->getUploadRootDir());
  43. }
  44. copy($this->getTmpUploadRootDir().$this->image, $this->getFullImagePath());
  45. unlink($this->getTmpUploadRootDir().$this->image);
  46. }
  47.  
  48. /**
  49.   * @ORM\PreRemove()
  50.   */
  51. public function removeImage()
  52. {
  53. unlink($this->getFullImagePath());
  54. rmdir($this->getUploadRootDir());
  55. }


Ten post edytował dopelganger 23.09.2015, 13:46:50
Go to the top of the page
+Quote Post
viking
post 23.09.2015, 16:25:49
Post #2





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Wstaw
  1. var_dump($this->image);

linię wcześniej.


--------------------
Go to the top of the page
+Quote Post
thek
post 23.09.2015, 19:40:52
Post #3





Grupa: Moderatorzy
Postów: 4 362
Pomógł: 714
Dołączył: 12.02.2009
Skąd: Jak się położę tak leżę :D




Ja bym już $this sprawdzał wink.gif


--------------------
Najpierw był manual... Jeśli tam nie zawarto słów mądrości to zapytaj wszechwiedzącego Google zadając mu własciwe pytania. A jeśli i on milczy to Twój problem nie istnieje :D
Go to the top of the page
+Quote Post
dopelganger
post 24.09.2015, 07:21:37
Post #4





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


Cytat(viking @ 23.09.2015, 17:25:49 ) *
Wstaw
  1. var_dump($this->image);

linię wcześniej.


pojawia mi się:

string(8) "test.jpg"
Go to the top of the page
+Quote Post
memory
post 24.09.2015, 07:48:28
Post #5





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

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


  1. $this->setImage($this->image->getClientOriginalName());


ustawiasz zmienną która powinna być UploadedFile stringiem. Usuń to

Powinno być wyżej

  1. /**
  2.   * Sets file.
  3.   *
  4.   * @param UploadedFile $file
  5.   */
  6. public function setImage(UploadedFile $image= null)
  7. {
  8. $this->image= $image;
  9. }


Dokładnie przeczytaj

http://symfony.com/doc/current/cookbook/do...le_uploads.html

Ten post edytował memory 24.09.2015, 07:49:09
Go to the top of the page
+Quote Post
dopelganger
post 24.09.2015, 08:09:53
Post #6





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


Cytat(memory @ 24.09.2015, 08:48:28 ) *
  1. $this->setImage($this->image->getClientOriginalName());


ustawiasz zmienną która powinna być UploadedFile stringiem. Usuń to

Powinno być wyżej

  1. /**
  2.   * Sets file.
  3.   *
  4.   * @param UploadedFile $file
  5.   */
  6. public function setImage(UploadedFile $image= null)
  7. {
  8. $this->image= $image;
  9. }


Dokładnie przeczytaj

http://symfony.com/doc/current/cookbook/do...le_uploads.html



nadal ten sam byk sad.gif
Go to the top of the page
+Quote Post
memory
post 24.09.2015, 08:14:58
Post #7





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

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


Podaj całą zawartość klasy
Go to the top of the page
+Quote Post
dopelganger
post 24.09.2015, 08:23:13
Post #8





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


Cytat(memory @ 24.09.2015, 09:14:58 ) *
Podaj całą zawartość klasy



  1. <?php
  2.  
  3. namespace Test\UserBundle\Entity;
  4.  
  5. use FOS\UserBundle\Entity\User as BaseUser;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8.  
  9.  
  10. /**
  11.  * @ORM\Entity
  12.  * @ORM\HasLifecycleCallbacks
  13.  * @ORM\Table(name="Test_users")
  14.  */
  15.  
  16. class User extends BaseUser
  17. {
  18. /**
  19.   * @ORM\Id
  20.   * @ORM\Column(type="integer")
  21.   * @ORM\GeneratedValue(strategy="AUTO")
  22.   */
  23. protected $id;
  24.  
  25. /**
  26.   * @var string $firstname
  27.   *
  28.   * @ORM\Column(name="firstname",type="string",length=30)
  29.   */
  30. protected $firstname;
  31.  
  32.  
  33.  
  34. /**
  35.   * @var string $image
  36.   * @Assert\File( maxSize = "15k", mimeTypes = {"image/jpeg", "image/jpg", "image/gif", "image/png", "image/bmp"}, mimeTypesMessage = "Prosze wybrać plik graficzny!")
  37. * @ORM\Column(name="image", type="string", length=255)
  38.   */
  39. protected $image;
  40.  
  41.  
  42.  
  43.  
  44.  
  45. /**
  46.   * Set firstname
  47.   *
  48.   * @param string $firstname
  49.   * @return User
  50.   */
  51. public function setFirstname($firstname)
  52. {
  53. $this->firstname = $firstname;
  54.  
  55. return $this;
  56. }
  57.  
  58.  
  59. /**
  60.   * Sets image.
  61.   *
  62.   * @param UploadedFile $image
  63.   */
  64. public function setImage(UploadedFile $image = null)
  65. {
  66. $this->image = $image;
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. /**
  76.   * Get id
  77.   *
  78.   * @return integer
  79.   */
  80. public function getId()
  81. {
  82. return $this->id;
  83. }
  84.  
  85.  
  86.  
  87. /**
  88.   * Get firstname
  89.   *
  90.   * @return string
  91.   */
  92. public function getFirstname()
  93. {
  94. return $this->firstname;
  95. }
  96.  
  97.  
  98.  
  99. /**
  100.   * Get image.
  101.   *
  102.   * @return UploadedFile
  103.   */
  104. public function getImage()
  105. {
  106. return $this->image;
  107. }
  108.  
  109.  
  110.  
  111.  
  112. public function __construct()
  113. {
  114. parent::__construct();
  115. // your own logic
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122. // Callbacks - upload ***********************************************************************
  123.  
  124.  
  125.  
  126. public function getFullImagePath() {
  127. return null === $this->image ? null : $this->getUploadRootDir(). $this->image;
  128. }
  129.  
  130. protected function getUploadRootDir() {
  131. // the absolute directory path where uploaded documents should be saved
  132. return $this->getTmpUploadRootDir().$this->getId()."/";
  133. }
  134.  
  135. protected function getTmpUploadRootDir() {
  136. // the absolute directory path where uploaded documents should be saved
  137. return __DIR__ . '/../../../../web/images/uploads/';
  138. }
  139.  
  140. /**
  141.   * @ORM\PrePersist()
  142.   * @ORM\PreUpdate()
  143.   */
  144. public function uploadImage() {
  145. // the file property can be empty if the field is not required
  146. if (null === $this->image) {
  147. return;
  148. }
  149. if(!$this->id){
  150. $this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
  151. }else{
  152. var_dump($this->image);
  153. $this->image->move($this->getUploadRootDir(), $this->image->getClientOriginalName());
  154. }
  155. $this->setImage($this->image->getClientOriginalName());
  156. }
  157.  
  158. /**
  159.   * @ORM\PostPersist()
  160.   */
  161. public function moveImage()
  162. {
  163. if (null === $this->image) {
  164. return;
  165. }
  166. if(!is_dir($this->getUploadRootDir())){
  167. mkdir($this->getUploadRootDir());
  168. }
  169. copy($this->getTmpUploadRootDir().$this->image, $this->getFullImagePath());
  170. unlink($this->getTmpUploadRootDir().$this->image);
  171. }
  172.  
  173. /**
  174.   * @ORM\PreRemove()
  175.   */
  176. public function removeImage()
  177. {
  178. unlink($this->getFullImagePath());
  179. rmdir($this->getUploadRootDir());
  180. }
  181.  
  182.  
  183.  
  184. }


zaznacze że zmieniając na:

setImage(UploadedFile $image = null)

w ogóle nie działa upload ani dodawanie do bazy nazwy pliku, wcześniejsza wersja działała
Go to the top of the page
+Quote Post
kapslokk
post 24.09.2015, 08:25:49
Post #9





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


  1. /**
  2.   * @var string $image
  3.   * @Assert\File( maxSize = "15k", mimeTypes = {"image/jpeg", "image/jpg", "image/gif", "image/png", "image/bmp"}, mimeTypesMessage = "Prosze wybrać plik graficzny!")
  4. * @ORM\Column(name="image", type="string", length=255)
  5.   */


Chyba już tutaj masz źle, wydaje mi się że $image powinno mieć tylko @Assert, bez kolumny w bazie itd. Zresztą tak jest nawet w tutorialu od symfony. Do bazy zapisujesz np ścieżkę na dysku w osobnym polu.

Ten post edytował kapslokk 24.09.2015, 08:26:38
Go to the top of the page
+Quote Post
dopelganger
post 24.09.2015, 08:29:23
Post #10





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


Cytat(kapslokk @ 24.09.2015, 09:25:49 ) *
  1. /**
  2.   * @var string $image
  3.   * @Assert\File( maxSize = "15k", mimeTypes = {"image/jpeg", "image/jpg", "image/gif", "image/png", "image/bmp"}, mimeTypesMessage = "Prosze wybrać plik graficzny!")
  4. * @ORM\Column(name="image", type="string", length=255)
  5.   */


Chyba już tutaj masz źle, wydaje mi się że $image powinno mieć tylko @Assert, bez kolumny w bazie itd. Zresztą tak jest nawet w tutorialu od symfony. Do bazy zapisujesz np ścieżkę na dysku w osobnym polu.



tylko że zapisuje tutaj nazwę pliku i to działa, działa również upload, wywala mi ten błąd po logowaniu i redirect na szablon profilu. Logowanie zadziała, ale musze cofnąć się do jakiejś innej strony i już wtedy jest ok.

Ten post edytował dopelganger 24.09.2015, 08:30:34
Go to the top of the page
+Quote Post
kapslokk
post 24.09.2015, 08:32:02
Post #11





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


Zapisuje, ale w 'image' przez to masz string, a nie obiekt. Na stringu nie zrobisz ->move, a na obiekcie owszem.
Go to the top of the page
+Quote Post
memory
post 24.09.2015, 08:38:41
Post #12





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

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


Przeczytaj dokładnie dokumentacje. Czemu do zmiennej $this->image przypisujesz dwie operacje ? zapis nazwy zdjecia oraz klase uploadedfile. Musisz to rozdzielić

  1. * @Assert\File( maxSize = "15k", mimeTypes = {"image/jpeg", "image/jpg", "image/gif", "image/png", "image/bmp"}, mimeTypesMessage = "Prosze wybrać plik graficzny!")
  2.  
  3. */
  4. protected $file;
  5.  
  6.  
  7. * @ORM\Column(name="image", type="string", length=255)
  8. */
  9. protected $image;
  10.  
  11. public function setImage($image )
  12. {
  13. $this->image = $image;
  14. }
  15.  
  16. public function setFile(UploadedFile $file= null)
  17. {
  18. $this->file= $file;
  19. }
  20.  
  21. /**
  22.   * @ORM\PrePersist()
  23.   * @ORM\PreUpdate()
  24.   */
  25. public function uploadImage() {
  26. // the file property can be empty if the field is not required
  27. if (null === $this->file) {
  28. return;
  29. }
  30. if(!$this->id){
  31. $this->file->move($this->getTmpUploadRootDir(), $this->file->getClientOriginalName());
  32. }else{
  33.  
  34. $this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName());
  35. }
  36. $this->setImage($this->file->getClientOriginalName());
  37. }
  38.  
  39. public function moveImage()
  40. {
  41. if (null === $this->file) {
  42. return;
  43. }
  44. if(!is_dir($this->getUploadRootDir())){
  45. mkdir($this->getUploadRootDir());
  46. }
  47. copy($this->getTmpUploadRootDir().$this->image, $this->getFullImagePath());
  48. unlink($this->getTmpUploadRootDir().$this->image);
  49. }
  50.  
  51.  
  52.  


I zmiana w createFormBuilder image na file

Ten post edytował memory 24.09.2015, 08:39:47
Go to the top of the page
+Quote Post
dopelganger
post 24.09.2015, 09:15:31
Post #13





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


wywala mi błąd:

Catchable Fatal Error: Argument 1 passed to Test\UserBundle\Entity\User::setFile() must be an instance of Test\UserBundle\Entity\UploadedFile, instance of Symfony\Component\HttpFoundation\File\UploadedFile given, called in /home/www2/tests.../vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 410 and defined

przywala się do
  1. public function setFile(UploadedFile $file= null) {


w CreateBuiler mam:
  1. $builder->add('file', null, array('data_class' => null, 'label' => 'form.file','translation_domain' => 'FOSUserBundle', 'required'=>false));
Go to the top of the page
+Quote Post
memory
post 24.09.2015, 09:42:19
Post #14





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

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


  1. $builder->add('file', 'file', array('data_class' => 'Test\UserBundle\Entity\User', 'label' => 'form.file','translation_domain' => 'FOSUserBundle', 'required'=>false));


Spróbuj tak
Go to the top of the page
+Quote Post
dopelganger
post 24.09.2015, 09:45:21
Post #15





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


Cytat(memory @ 24.09.2015, 10:42:19 ) *
  1. $builder->add('file', 'file', array('data_class' => 'Test\UserBundle\Entity\User', 'label' => 'form.file','translation_domain' => 'FOSUserBundle', 'required'=>false));


Spróbuj tak


nadal wyskakuje błąd
Go to the top of the page
+Quote Post
ohm
post 24.09.2015, 12:29:20
Post #16





Grupa: Zarejestrowani
Postów: 618
Pomógł: 143
Dołączył: 22.12.2010

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


Kod
use Symfony\Component\HttpFoundation\File\UploadedFile;

?
Go to the top of the page
+Quote Post
dopelganger
post 24.09.2015, 13:22:15
Post #17





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


Cytat(ohm @ 24.09.2015, 13:29:20 ) *
Kod
use Symfony\Component\HttpFoundation\File\UploadedFile;

?


po tym zabiegu błąd się nie pojawia, ale nie zapisuje nazwy pliku do bazy, ani nie wgrywa pliku na serwer
Go to the top of the page
+Quote Post
kpt_lucek
post 24.09.2015, 13:41:27
Post #18





Grupa: Zarejestrowani
Postów: 428
Pomógł: 77
Dołączył: 10.07.2011
Skąd: Warszawa

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


Eh

Rozdziel to na 2 przypadki:
1. masz model który jest klasą DTO i taki powinien zostać, czyste get,set,add,remove, żadnych operacji na plikach.
2. masz usługę która na podstawie w/w obiektu ogarnia save/remove/move itd. obrazka

Dzięki temu oddzielisz logikę od modelu i obie drogi będziesz mógł rozwijać niezależnie, bo po co sobie utrudniać?

P.S.

Zaintersuj się bibliotekami do zarządzania obrazkami, jest ich masa na GH i/lub Packagist

Ten post edytował kpt_lucek 24.09.2015, 14:05:02


--------------------


Cytat
There is a Bundle for that
Lukas Kahwe Smith - October 31th, 2014
Go to the top of the page
+Quote Post
dopelganger
post 8.10.2015, 12:00:58
Post #19





Grupa: Zarejestrowani
Postów: 236
Pomógł: 0
Dołączył: 27.10.2012

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


cześć
sory, ale wracam jeszcze do tego tematu.
Przetestowałem 3 różne sposoby tego uploadu łącznie z tym który tutaj wrzuciłem a potem poprawiłem wg waszych wskazówek i znalazłem pewną (błędną?) prawidłowość w każdym ze sposobów:

a więc:
edytuje profil usera, wskazuje obrazek z dysku, zapisuje dane i JEST OK, => ALE KIEDY ponownie to robie dla danego usera już nie zapisuje obrazka do bazy ani go nie uploaduje, nawet kiedy usune nazwę pliku z bazy :/
I ZNOWU kiedy tworze nowy / kolejny profil a potem aktualizuje jego dane - plik wczytuje na serwer i zapisuje do bazy smile.gif - ale udaje się tylko za pierwszym razem.. itd itd itd
taka pętla...

to dotyczy jakiegokolwiek przykładu wg którego się wzoruję lub testuje smile.gif - łącznie z manualem http://symfony.com/doc/current/cookbook/do...le_uploads.html


dziwne zjawisko smile.gif

może ktoś coś wie ?

Ten post edytował dopelganger 8.10.2015, 12:39:12
Go to the top of the page
+Quote Post

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: 16.04.2024 - 17:16