Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony] Porblem z json
rychu123
post
Post #1





Grupa: Zarejestrowani
Postów: 194
Pomógł: 1
Dołączył: 18.09.2008

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


Witam, mam pewien problem. Wyciągam dane z bazy i chce wrzuci dane do json. Problem jest w tym ze dostaje puste dane w json tzn


[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{
},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{
},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{
},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{
},{},{},{},{},{},{},{}]


  1. $e = $this->getDoctrine()->getManager();
  2.  
  3. $clients = $e->getRepository('AppBundle:Client')->findAll();
  4.  
  5. // return $this->render('client/index.html.twig', array(
  6. //'clients' => $clients ));
  7.  
  8. return new Response(json_encode($clients), Response::HTTP_OK, array(
  9. 'Content-Type' => 'application/json' ));
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 9)
Turson
post
Post #2





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Nie możesz ot tak zrobić JSONa z encji. Pobierz z repozytorium tablicę (array) i ją przekonwertuj do jsona
Albo zobacz http://php.net/manual/pl/class.jsonserializable.php
Go to the top of the page
+Quote Post
rychu123
post
Post #3





Grupa: Zarejestrowani
Postów: 194
Pomógł: 1
Dołączył: 18.09.2008

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


przecież $clients jest tablica
Go to the top of the page
+Quote Post
Turson
post
Post #4





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Ale każdy element tablicy jest encją, chyba że repo robi co innego
Go to the top of the page
+Quote Post
rychu123
post
Post #5





Grupa: Zarejestrowani
Postów: 194
Pomógł: 1
Dołączył: 18.09.2008

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


Oświeć mnie bo bez kitu ale niewiem

dump mi zwraca

array:115 [?
0 => Client {#1178 ?}
1 => Client {#1180 ?}
2 => Client {#1181 ?}

Ten post edytował rychu123 13.05.2016, 19:35:06
Go to the top of the page
+Quote Post
Turson
post
Post #6





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Niech AppBundle:Client imlementuje http://php.net/manual/pl/class.jsonserializable.php i udostępnia metodę, jak zrobić jsona z klasy
Go to the top of the page
+Quote Post
rychu123
post
Post #7





Grupa: Zarejestrowani
Postów: 194
Pomógł: 1
Dołączył: 18.09.2008

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


NIe wiem, nie znam symfony 2
Go to the top of the page
+Quote Post
Turson
post
Post #8





Grupa: Zarejestrowani
Postów: 4 291
Pomógł: 829
Dołączył: 14.02.2009
Skąd: łódź

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


Tu nie chodzi o znajomość Symfony.

  1. class Client implements JsonSerializable{
  2.  
  3.  
  4. protected $id;
  5.  
  6. protected $name;
  7.  
  8. public function jsonSerialize(){
  9. return [
  10. 'id' => $this->id,
  11. 'name' => $this->name,
  12. ];
  13. }
  14.  
  15. }
Go to the top of the page
+Quote Post
rychu123
post
Post #9





Grupa: Zarejestrowani
Postów: 194
Pomógł: 1
Dołączył: 18.09.2008

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


a niemożna tego zrobić w controller?
Go to the top of the page
+Quote Post
kapslokk
post
Post #10





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

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


Można, np tym: http://symfony.com/doc/current/components/serializer.html

Co nie zmienia faktu, że metoda, którą podał Turson zdaje się być lepsza (IMG:style_emoticons/default/smile.gif)

Ten post edytował kapslokk 13.05.2016, 20:18:43
Go to the top of the page
+Quote Post

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: 16.10.2025 - 00:04