<tfoot id='lyBgu'></tfoot>

        <legend id='lyBgu'><style id='lyBgu'><dir id='lyBgu'><q id='lyBgu'></q></dir></style></legend>

        <small id='lyBgu'></small><noframes id='lyBgu'>

          <bdo id='lyBgu'></bdo><ul id='lyBgu'></ul>

      1. <i id='lyBgu'><tr id='lyBgu'><dt id='lyBgu'><q id='lyBgu'><span id='lyBgu'><b id='lyBgu'><form id='lyBgu'><ins id='lyBgu'></ins><ul id='lyBgu'></ul><sub id='lyBgu'></sub></form><legend id='lyBgu'></legend><bdo id='lyBgu'><pre id='lyBgu'><center id='lyBgu'></center></pre></bdo></b><th id='lyBgu'></th></span></q></dt></tr></i><div id='lyBgu'><tfoot id='lyBgu'></tfoot><dl id='lyBgu'><fieldset id='lyBgu'></fieldset></dl></div>

        Doctrine 向 DQL 的 NEW 构造函数发送一个实体

        Doctrine send an entity to DQL#39;s NEW constructor(Doctrine 向 DQL 的 NEW 构造函数发送一个实体)

        • <i id='RmUk0'><tr id='RmUk0'><dt id='RmUk0'><q id='RmUk0'><span id='RmUk0'><b id='RmUk0'><form id='RmUk0'><ins id='RmUk0'></ins><ul id='RmUk0'></ul><sub id='RmUk0'></sub></form><legend id='RmUk0'></legend><bdo id='RmUk0'><pre id='RmUk0'><center id='RmUk0'></center></pre></bdo></b><th id='RmUk0'></th></span></q></dt></tr></i><div id='RmUk0'><tfoot id='RmUk0'></tfoot><dl id='RmUk0'><fieldset id='RmUk0'></fieldset></dl></div>
            <bdo id='RmUk0'></bdo><ul id='RmUk0'></ul>
            <tfoot id='RmUk0'></tfoot>

                <tbody id='RmUk0'></tbody>

                <legend id='RmUk0'><style id='RmUk0'><dir id='RmUk0'><q id='RmUk0'></q></dir></style></legend>

                  <small id='RmUk0'></small><noframes id='RmUk0'>

                  本文介绍了Doctrine 向 DQL 的 NEW 构造函数发送一个实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 Symfony2 中有一个带有 Doctrine 的博客应用程序,由三个实体组成

                  I have a Blog application in Symfony2 with Doctrine, made of three entities

                  • 发布
                  • 评论
                  • 用户

                  一个帖子可以有很多评论

                  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,我不想要因为 Post 可以包含巨大的文本,所以我只对 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

                  http://docs.doctrine-project.org/en/latest/reference/dql-doctrine-query-language.html#new-operator-syntax

                  所以现在我的 DQL 查询看起来像这样

                  so now my DQL query looks like that

                  <代码>SELECT NEW CommentDTO(c) FROM Comment as c WHERE c.author = :user

                  CommentDTO 的构造函数是这样的

                  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"
                  
                  }
                  

                  当然我可以一个一个地给出参数,但是 Comment 有十几个字段,并且一个一个地发送它们看起来非常 hackish.

                  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:

                  请注意,您只能将标量表达式传递给构造函数.

                  Note that you can only pass scalar expressions to the constructor.

                  我在 问题跟踪器中找不到相关的功能请求,因此很可能在可预见的将来不会实施.

                  I could not find a related feature request in the issue tracker, so it will likely not be implemented in the foreseeable future.

                  这篇关于Doctrine 向 DQL 的 NEW 构造函数发送一个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  • <bdo id='TwEJL'></bdo><ul id='TwEJL'></ul>

                    <i id='TwEJL'><tr id='TwEJL'><dt id='TwEJL'><q id='TwEJL'><span id='TwEJL'><b id='TwEJL'><form id='TwEJL'><ins id='TwEJL'></ins><ul id='TwEJL'></ul><sub id='TwEJL'></sub></form><legend id='TwEJL'></legend><bdo id='TwEJL'><pre id='TwEJL'><center id='TwEJL'></center></pre></bdo></b><th id='TwEJL'></th></span></q></dt></tr></i><div id='TwEJL'><tfoot id='TwEJL'></tfoot><dl id='TwEJL'><fieldset id='TwEJL'></fieldset></dl></div>
                  • <small id='TwEJL'></small><noframes id='TwEJL'>

                      • <legend id='TwEJL'><style id='TwEJL'><dir id='TwEJL'><q id='TwEJL'></q></dir></style></legend>

                              <tbody id='TwEJL'></tbody>
                            <tfoot id='TwEJL'></tfoot>