I have a Blog application in Symfony2 with Doctrine, made of three entities
- Post
- Comment
User
one Post can have many Comments
- one User can have many comments
this application has an json API call "/me/comments"
I would like it to return
[
{
'text': 'great post',
... 10 other field a comment can have ...
'post': { 'id': 3}
},
{
'text': 'i dont like it',
... 10 other field a comment can have ...
'post': {'id': 4}
},
]
The normal doctrine function returns me all the relation (User, Post) in the json, which I don't want as Post can contains huge text, so i'm only interested in the Id
I've tried many solutions and I've found that one
http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#new-operator-syntax
so now my DQL query looks like that
SELECT NEW CommentDTO(c) FROM Comment as c WHERE c.author = :user
the constructor of CommentDTO looks like that
__construct(Comment c) {
$this->text = c.getText();
$this->post = [ 'id' => c.getPost().getId() ];
}
when I execute it I got
{
"code":500,
"message":"Argument 1 passed to MVMS\\ApiBundle\\Entity\\CommentDTO::__construct() must be an instance of MVMS\\ApiBundle\\En
tity\\Comment, integer given"
}
of course I could give the parameter one by one but Comment has a dozen of fields and sending them one by one seems highly hackish.
Is there a way to send the object directly ?