问题描述
我在Symfony2中有一个由Doctrine编写的Blog应用程序,它由三个实体组成
I have a Blog application in Symfony2 with Doctrine, made of three entities
- Post
- 评论
-
用户
- Post
- Comment
User
一个帖子可以有很多评论
one Post can have many Comments
此应用程序具有json API调用 / me / comments
this application has an json API call "/me/comments"
我希望它返回
[
{
'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}
},
]
正常的学说功能返回我在json中的所有关系(用户,发布),我不希望这样做,因为发布可以包含大量文本,所以我只对ID感兴趣
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
所以现在我的DQL查询看起来像这样
so now my DQL query looks like that
从注释中选择新的CommentDTO(c)作为c在哪里。 author =:user
CommentDTO的构造函数如下
the constructor of CommentDTO looks like that
__construct(Comment c) {
$this->text = c.getText();
$this->post = [ 'id' => c.getPost().getId() ];
}
我执行该程序时
{
"code":500,
"message":"Argument 1 passed to MVMS\\ApiBundle\\Entity\\CommentDTO::__construct() must be an instance of MVMS\\ApiBundle\\En
tity\\Comment, integer given"
}
当然我可以一个一个地给参数,但是Comment有十二个字段并一个一地发送
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 ?
推荐答案
如@Cerad在评论中所述,根据,即在2017年仍然有意义:
As mentioned by @Cerad in the comments, there is no current way to achieve this, according to the documentation, which is still relevant in 2017:
我在,因此在可预见的将来可能不会实现。
I could not find a related feature request in the issue tracker, so it will likely not be implemented in the foreseeable future.
这篇关于主义向DQL的NEW构造函数发送实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!