Symfony how the data read by FindAll is turned into JSON

Source: Internet
Author: User
$repository = $this->getdoctrine ()->getrepository (' Appbundle:user '); $all = $repository->findall ();


Array (size=2)  0 =     object (appbundle\entity\user) [248]      private ' id ' = = int 1      private ' name ' = > String ' A Foo Bar ' (length=9)      private ' pass ' = String ' 19.99 ' (length=5)  1 =     object (appbundle\en Tity\user) [251]      private ' id ' = = int 2      private ' name ' = ' + string ', ' Fot Bar ' (length=11)      private ' pass ' = = String ' 40.00 ' (length=5)


The data returned is this, and now how does this turn into JSON data
I now use return new Jsonresponse ($all); [{}]


Reply to discussion (solution)

You need to implement the Jsonserializable interface for Appbundle\entity\user.
Such as

Class T implements jsonserializable {  private $id;  Private $name;  Private $pass;  function __construct ($id, $name, $pass) {    $this->id = $id;    $this->name = $name;    $this->pass = $pass;  }  function jsonserialize () {    return array (      ' id ' = = $this->id,      ' name ' = = $this->name,      ' Pass ' = = $this->pass,      );}  } $d [] = new T (1, ' A ', ' P '), $d [] = new T (2, ' B ', ' P '); echo Json_encode ($d);
[{"id": 1, "name": "A", "Pass": "P"},{"id": 2, "name": "B", "Pass": "P"}]
Otherwise it can only be [{},{}]
Because the returned property is private

You need to implement the Jsonserializable interface for Appbundle\entity\user.
Such as

Class T implements jsonserializable {  private $id;  Private $name;  Private $pass;  function __construct ($id, $name, $pass) {    $this->id = $id;    $this->name = $name;    $this->pass = $pass;  }  function jsonserialize () {    return array (      ' id ' = = $this->id,      ' name ' = = $this->name,      ' Pass ' = = $this->pass,      );}  } $d [] = new T (1, ' A ', ' P '), $d [] = new T (2, ' B ', ' P '); echo Json_encode ($d);
[{"id": 1, "name": "A", "Pass": "P"},{"id": 2, "name": "B", "Pass": "P"}]
Otherwise it can only be [{},{}]
Because the returned property is private



Thanks, because the property is private.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.