• <bdo id='pDWKT'></bdo><ul id='pDWKT'></ul>

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

    1. <tfoot id='pDWKT'></tfoot><legend id='pDWKT'><style id='pDWKT'><dir id='pDWKT'><q id='pDWKT'></q></dir></style></legend>

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

      1. Symfony2/Doctrine 中两列的查询生成器和分组依据生成重复项

        Query Builder and Group By on two columns in Symfony2 / Doctrine generate duplicates(Symfony2/Doctrine 中两列的查询生成器和分组依据生成重复项)
          <tbody id='H8GCd'></tbody>

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

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

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

                2. <tfoot id='H8GCd'></tfoot>

                3. <legend id='H8GCd'><style id='H8GCd'><dir id='H8GCd'><q id='H8GCd'></q></dir></style></legend>

                  本文介绍了Symfony2/Doctrine 中两列的查询生成器和分组依据生成重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在创建一个消息包,其中的消息按联系人分组.在我的索引页面上,我显示了不同的线程.当您单击一个线程时,它会显示您与联系人之间交换的所有消息.我使用查询生成器在我的索引页面上显示线程:

                  I'm creating a message bundle where messages are grouped per contacts. On my index page, I display different threads. When you clic on one thread, it show all the messages exchanged between you and your contact. I use a Query Builder to display the threads on my index page:

                  $qb = $this->createQueryBuilder('m')
                      ->where('m.from = ?1 or m.to = ?1')
                      ->groupBy('m.to, m.from')
                      ->orderBy('m.date', 'DESC')
                      ->setParameter(1, $user->getId())
                      ->setMaxResults($pagination) // limit
                      ->setFirstResult($pagination * $page) // offset
                  ;
                  

                  如果我有 3 个条目,例如:

                  If I have 3 entries, for exemple:

                  +----+------+----+
                  | id | from | to |
                  +----+------+----+
                  | 1  | 1    | 2  |
                  +----+------+----+
                  | 2  | 2    | 1  |
                  +----+------+----+
                  | 3  | 1    | 2  |
                  +----+------+----+
                  

                  我希望:

                  +----+------+----+
                  | id | from | to |
                  +----+------+----+
                  | 3  | 1    | 2  |
                  +----+------+----+
                  

                  但我明白了:

                  +----+------+----+
                  | id | from | to |
                  +----+------+----+
                  | 2  | 2    | 1  |
                  +----+------+----+
                  | 3  | 1    | 2  |
                  +----+------+----+
                  

                  我找到了一种用 SQL 实现的方法,对 from_id 和 to_id 使用相同的别名:

                  I found a way to do it with SQL, using the same alias for from_id and to_id:

                  SELECT id, from_id as c, to_id as c FROM Message WHERE c = 1 GROUP BY from_id, to_id
                  

                  但我不知道如何使用 Doctrine.

                  But I don't know how to do it with Doctrine.

                  在我有了更好的主意之前,我会使用一个键来轻松地分组".

                  Until I get a better idea, I use a key to easily "group by".

                  // entity
                  
                  /**
                  * @ORMColumn(name="key", type="string", length=40)
                  */
                  private $key;
                  
                  /**
                   * @ORMPrePersist()
                   */
                  public function setOnPrePersist()
                  {
                      if($this->from < $this->to) {
                          $key = $this->from . 't' . $this->to;
                      } else {
                          $key = $this->to . 't' . $this->from;
                      }
                  
                      $this->key = $key;
                  }
                  
                  // query builder
                  
                  $qb = $this->createQueryBuilder('m')
                      ->where('m.from = ?1 or m.to = ?1')
                      ->groupBy('m.key')
                      ->orderBy('m.date', 'DESC')
                      ->setParameter(1, $user->getId())
                      ->setMaxResults($pagination) // limit
                      ->setFirstResult($pagination * $page) // offset
                  ;
                  
                  return $qb->getQuery()->getResult();
                  

                  推荐答案

                  如果'group by'中有很多列,则必须使用 addGroupBy() 方法.

                  in case that you have many columns in 'group by' you must use addGroupBy() method.

                  $qb = $this->createQueryBuilder('m')
                      ->where('m.from = ?1 or m.to = ?1')
                      ->groupBy('m.to')
                      ->addGroupBy('m.from')
                      ->orderBy('m.date', 'DESC')
                      ->setParameter(1, $user->getId())
                      ->setMaxResults($pagination) // limit
                      ->setFirstResult($pagination * $page) // offset
                  ;
                  

                  :)

                  这篇关于Symfony2/Doctrine 中两列的查询生成器和分组依据生成重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 的问题)

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

                  <tfoot id='H7ruB'></tfoot>
                  <legend id='H7ruB'><style id='H7ruB'><dir id='H7ruB'><q id='H7ruB'></q></dir></style></legend>

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

                        • <bdo id='H7ruB'></bdo><ul id='H7ruB'></ul>

                              <tbody id='H7ruB'></tbody>