Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony]SF2 return json...
Szymciosek
post
Post #1





Grupa: Zarejestrowani
Postów: 1 168
Pomógł: 126
Dołączył: 5.02.2010
Skąd: Świdnica

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


Witam,
chciałem zobaczyć jak Symfony2 poradzi sobie jako API i w tym celu chciałem wykorzystać ActionScript3.0 + Symfony2 (PHP) więc mam sobie jakąś aplikację, która wysyła z flasha json ( { id:1 } )
więc flash oczekuje wszystkich kolumn, które należą do ID = 1. Jako z tym nie mam problemu, bo normalnie mogę zrobić to tak:

  1. $json = json_decode($_POST['data']); //odebranie json z flash
  2.  
  3. $query = mysql_query("SELECT * FROM user WHERE id=$json->id"); //zapytanie do bazy
  4.  
  5. while($data = mysql_fetch_object($query))
  6. {
  7. $object = array(
  8. 'user' => $data,
  9. );
  10. } //uzupełnienie tablicy $object danymi z bazy
  11. print json_encode($object); //zwrócenie json do flasha


ale jak to samo zrobić w Symfony2 ?

  1. <?php
  2.  
  3. namespace SimonMedia\ApiBundle\Controller;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  8.  
  9. class DefaultController extends Controller
  10. {
  11. /**
  12.   * @Route("/")
  13.   * @Template()
  14.   */
  15. public function indexAction()
  16. {
  17. return array();
  18. }
  19.  
  20. /**
  21.   * @Route("/user/get/all")
  22.   */
  23. public function getAll()
  24. {
  25. $em = $this->getDoctrine()->getEntityManager();
  26. $entities = $em->getRepository('SimonMediaApiBundle:Userzy')->findAll();
  27.  
  28. var_dump($entities);
  29. }
  30. }


var_dump zwraca
  1. array (size=3)
  2. 0 =>
  3. object(SimonMedia\ApiBundle\Entity\Userzy)[211]
  4. private 'id' => int 1
  5. private 'name' => string 'Janusz' (length=6)
  6. private 'email' => string 'Janko@wp.pl' (length=11)
  7. private 'phone' => string '668559634' (length=9)
  8. 1 =>
  9. object(SimonMedia\ApiBundle\Entity\Userzy)[206]
  10. private 'id' => int 2
  11. private 'name' => string 'Marek' (length=5)
  12. private 'email' => string 'Maro@wp.pl' (length=10)
  13. private 'phone' => string '665583999' (length=9)
  14. 2 =>
  15. object(SimonMedia\ApiBundle\Entity\Userzy)[207]
  16. private 'id' => int 3
  17. private 'name' => string 'Mariusz' (length=7)
  18. private 'email' => string 'Mario@wp.pl' (length=11)
  19. private 'phone' => string '994888777' (length=9)


Ale nijak nie potrafię się po tym ruszyć...

EDIT::
Doszedłem do tego, że po zmianie w klasie Userzy.php (entity) z private na public mam dostęp do np

print $entities[0]->name;

w przeciwnym razie otrzymuję error
Cannot access private property SimonMedia\ApiBundle\Entity\Userzy::$name in F:\WORK\labs\Symfony\ApiID\src\SimonMedia\ApiBundle\Controller\DefaultController.php on line 28

Ale chyba taka zmiana nie jest dobra i nie na tym to ma polegać ?

Ten post edytował Szymciosek 16.09.2012, 11:06:44
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Szymciosek
post
Post #2





Grupa: Zarejestrowani
Postów: 1 168
Pomógł: 126
Dołączył: 5.02.2010
Skąd: Świdnica

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


Owszem mam.

  1. Owszem mam.
  2.  
  3. <?php
  4.  
  5. namespace SimonMedia\ApiBundle\Entity;
  6.  
  7. use Doctrine\ORM\Mapping as ORM;
  8.  
  9. /**
  10.  * SimonMedia\ApiBundle\Entity\Userzy
  11.  *
  12.  * @ORM\Table()
  13.  * @ORM\Entity(repositoryClass="SimonMedia\ApiBundle\Entity\UserzyRepository")
  14.  */
  15. class Userzy
  16. {
  17. /**
  18.   * @var integer $id
  19.   *
  20.   * @ORM\Column(name="id", type="integer")
  21.   * @ORM\Id
  22.   * @ORM\GeneratedValue(strategy="AUTO")
  23.   */
  24. private $id;
  25.  
  26. /**
  27.   * @var string $name
  28.   *
  29.   * @ORM\Column(name="name", type="string", length=255)
  30.   */
  31. private $name;
  32.  
  33. /**
  34.   * @var string $email
  35.   *
  36.   * @ORM\Column(name="email", type="string", length=255)
  37.   */
  38. private $email;
  39.  
  40. /**
  41.   * @var string $phone
  42.   *
  43.   * @ORM\Column(name="phone", type="string", length=255)
  44.   */
  45. private $phone;
  46.  
  47.  
  48. /**
  49.   * Get id
  50.   *
  51.   * @return integer
  52.   */
  53. public function getId()
  54. {
  55. return $this->id;
  56. }
  57.  
  58. /**
  59.   * Set name
  60.   *
  61.   * @param string $name
  62.   */
  63. public function setName($name)
  64. {
  65. $this->name = $name;
  66. }
  67.  
  68. /**
  69.   * Get name
  70.   *
  71.   * @return string
  72.   */
  73. public function getName()
  74. {
  75. return $this->name;
  76. }
  77.  
  78. /**
  79.   * Set email
  80.   *
  81.   * @param string $email
  82.   */
  83. public function setEmail($email)
  84. {
  85. $this->email = $email;
  86. }
  87.  
  88. /**
  89.   * Get email
  90.   *
  91.   * @return string
  92.   */
  93. public function getEmail()
  94. {
  95. return $this->email;
  96. }
  97.  
  98. /**
  99.   * Set phone
  100.   *
  101.   * @param string $phone
  102.   */
  103. public function setPhone($phone)
  104. {
  105. $this->phone = $phone;
  106. }
  107.  
  108. /**
  109.   * Get phone
  110.   *
  111.   * @return string
  112.   */
  113. public function getPhone()
  114. {
  115. return $this->phone;
  116. }
  117. }
Go to the top of the page
+Quote Post

Posty w temacie
- Szymciosek   [Symfony]SF2 return json...   16.09.2012, 10:43:09
- - marcio   Pokaz klase entity, powinienes w niej miec gettery...   16.09.2012, 12:17:30
- - Szymciosek   Owszem mam. [PHP] pobierz, plaintext Owszem mam....   16.09.2012, 12:48:12
- - marcio   Nie rozumiem. Robisz petle na obiekcie i potem wy...   16.09.2012, 12:54:51
- - Szymciosek   Czyli jak ? Mam problem nawet z dostaniem się do p...   16.09.2012, 13:00:11
- - m44   Tworzysz sobie w repozytorium "Userzy" m...   16.09.2012, 13:09:17
- - Szymciosek   Możesz powiedzieć coś więcej o tej metodzie ? Pot...   16.09.2012, 13:56:39
- - phpion   Podstawy budowy tablic się kłaniają... [PHP] pobie...   16.09.2012, 14:09:35
- - Szymciosek   Z takim problemem sobie poradziłem i działa ok dzi...   19.09.2012, 19:43:18
- - marcio   Tak na szybko: [PHP] pobierz, plaintext foreach ...   20.09.2012, 07:12:38
- - phpion   Może warto najpierw zapoznać się z podstawami PHP,...   20.09.2012, 08:20:50
- - Szymciosek   Niestety, próbowałem już chyba na wszystkie sposob...   20.09.2012, 16:41:54
- - phpion   Spróbuj tego: [PHP] pobierz, plaintext $data = arr...   20.09.2012, 18:02:57
- - Szymciosek   Działa bardzo dobrze, ale chyba nie wpadłbym na to...   20.09.2012, 18:14:11
- - netrat   Osobiście sugerowałbym wpierw zbudowanie odpowiedn...   1.10.2012, 20:58:23
- - Szymciosek   No a jak wtedy wygląda metoda toArray ?   1.10.2012, 21:07:50
- - ano   Zamiast ręcznie transformować obiekty na tablice, ...   7.10.2012, 13:09:12
- - Szymciosek   Witam ponownie, nie chciałem zakładać nowego temat...   13.10.2012, 12:17:46
- - m44   To wszystko jest opisane w dokumentacji Doctrine. ...   13.10.2012, 19:24:34
- - Szymciosek   Właśnie chodzi o to czy kolumna istnieje w bazie, ...   14.10.2012, 00:47:05


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: 6.10.2025 - 15:28