$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.