When I get a list of entities from Doctrine, how can I return different json 'views' ?
Ie. when I load Student classes I want views with only id, displayName
and another with more details id, firstName, lastName, email, groups
.
Groups is another collection with id, name
.
I'm using lumen and doctrine.
Asked
Active
Viewed 90 times
0

BakGat
- 661
- 2
- 5
- 19
-
Possible duplicate of [Serializing PHP object to JSON](http://stackoverflow.com/questions/6836592/serializing-php-object-to-json) – goto Jun 29 '16 at 12:17
-
@goto I don't believe it is a duplicate. Your link provides information on serializing all fields. I just want to serialize some fields. And even more: I must be able to serialize two fields for request A, and 5 fields for request B. – BakGat Jun 29 '16 at 13:06
1 Answers
0
You can implement JsonSerializable interface on your entityes and create custom versions on it. ie:
class MyEntity implements JsonSerializable {
/**
* Doctrine annotation
*/
private $myEntityMember;
...
public function jsonSerialize() {
//serialize your entity here and use custom params to modify the serialized object output
}
}
This way you can call $myEntityObject->jsonSerialize()
whenever you need.

fkupper
- 520
- 3
- 9