Symfony 2 Doctrine exports to JSON

I am using Symfony 2 with Doctrine 2 to create a web service (JSON) for an iOS application.

To get my essence, I do:

$articles = $this->getDoctrine()->getRepository('UdoPaddujourBundle:MenuArticle')->findAll(); 

I must tell you that:

 $article = array(); $article = $articles->toArray(); 

Gives me the following error:

 Fatal error: Call to a member function toArray() on a non-object 

The same thing happens with

 $article = $articles->exportTo('json'); 



How can I create a json response?

Regards, Cearnau Dan

Edit: var_dump ($ articles) =

 array(18) { [0]=> object(Udo\PaddujourBundle\Entity\MenuArticle)#50 (4) { ["id":"Udo\PaddujourBundle\Entity\MenuArticle":private]=> int(1) ["name":"Udo\PaddujourBundle\Entity\MenuArticle":private]=> string(17) "My Article Name 1" ["description":"Udo\PaddujourBundle\Entity\MenuArticle":private]=> string(26) "My Article Description 1" ["price":"Udo\PaddujourBundle\Entity\MenuArticle":private]=> float(20) } [1]=> ... 

- LATER EDIT

How can I view all "property names"? This is what I have:

 $myarray=array(); $myArray["name"]=array(); $myArray["description"]=array(); foreach($articles in $article) { array_push($myArray["name"], $article->getName()); array_push($myArray["description"], $article->getDescription()); } 
+6
json php web-services symfony doctrine
Aug 10 2018-11-11T00:
source share
3 answers

If you use the doctrine request, you can also do this:

  $em = $this->getDoctrine()->getEntityManager(); $query = $em->createQuery('SELECT ma FROM UdoPaddujourBundle:MenuArticle ma ...etc'); $myArray = $query->getArrayResult(); 

and then json_encode ($ myArray); See here for more details.

+14
Aug 11 2018-11-11T00:
source share

If you come from the background of symfony 1.x, for entities, including helpers, for converting to arrays, etc. much more magic was available.

In Symfony2, most of the magic has disappeared; Entities, in particular, are now simple old PHP objects managed by Doctrine 2 to be stored in the database, which means that in order for your object to have methods such as toArray() , you must implement them yourself. This should be pretty trivial: just return an array of key values ​​with ("name of property" => "value of property") ... if you have relationships configured with other objects, you also need to implement the toArray() method for them, and just call it from the main object when converting.

Then, as soon as you have your array of objects, $json = json_encode($array); will provide you a JSON string to send as a response.

+3
Aug 10 '11 at 16:21
source share

You can use json_encode($articles) when an object (or any other object) implements JsonSerializable :

 <?php namespace My\AppBundle\Entity; use JsonSerializable; class Channel implements JsonSerializable { /* * All your fields, getters and setters. */ /** * Returns serializable items. * * @return array */ public function jsonSerialize() { return [ 'name' => $this->getName(), 'description' => $this->getDescription(), ]; } } 
0
Aug 05 '16 at 2:04 on
source share



All Articles