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

  • <tfoot id='SScPI'></tfoot>

  • <small id='SScPI'></small><noframes id='SScPI'>

  • <legend id='SScPI'><style id='SScPI'><dir id='SScPI'><q id='SScPI'></q></dir></style></legend>
        <bdo id='SScPI'></bdo><ul id='SScPI'></ul>

      1. “我们"一个 DoctrineCommonCollectionsArrayCollection?

        quot;usortquot; a DoctrineCommonCollectionsArrayCollection?(“我们一个 DoctrineCommonCollectionsArrayCollection?)
          <tbody id='8Yau8'></tbody>
          <bdo id='8Yau8'></bdo><ul id='8Yau8'></ul>

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

          2. <legend id='8Yau8'><style id='8Yau8'><dir id='8Yau8'><q id='8Yau8'></q></dir></style></legend>
                • <tfoot id='8Yau8'></tfoot>
                  本文介绍了“我们"一个 DoctrineCommonCollectionsArrayCollection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在各种情况下,我需要根据对象中的属性对 DoctrineCommonCollectionsArrayCollection 进行排序.没有找到立即执行此操作的方法,我这样做了:

                  In various cases I need to sort a DoctrineCommonCollectionsArrayCollection according to a property in the object. Without finding a method doing that right away, I do this:

                  // $collection instanceof DoctrineCommonCollectionsArrayCollection
                  $array = $collection->getValues();
                  usort($array, function($a, $b){
                      return ($a->getProperty() < $b->getProperty()) ? -1 : 1 ;
                  });
                  
                  $collection->clear();
                  foreach ($array as $item) {
                      $collection->add($item);
                  }
                  

                  当您必须将所有内容复制到本机 PHP 数组并返回时,我认为这不是最佳方法.我想知道是否有更好的方法来配置"DoctrineCommonCollectionsArrayCollection.我想念任何文档吗?

                  I presume this is not the best way when you have to copy everything to native PHP array and back. I wonder if there is a better way to "usort" a DoctrineCommonCollectionsArrayCollection. Do I miss any doc?

                  推荐答案

                  要对现有集合进行排序,您正在寻找 ArrayCollection::getIterator() 方法,该方法返回一个 ArrayIterator.例子:

                  To sort an existing Collection you are looking for the ArrayCollection::getIterator() method which returns an ArrayIterator. example:

                  $iterator = $collection->getIterator();
                  $iterator->uasort(function ($a, $b) {
                      return ($a->getPropery() < $b->getProperty()) ? -1 : 1;
                  });
                  $collection = new ArrayCollection(iterator_to_array($iterator));
                  

                  最简单的方法是让存储库中的查询处理您的排序.

                  The easiest way would be letting the query in the repository handle your sorting.

                  假设您有一个 SuperEntity,它与 Category 实体存在多对多关系.

                  Imagine you have a SuperEntity with a ManyToMany relationship with Category entities.

                  然后例如创建这样的存储库方法:

                  Then for instance creating a repository method like this:

                  // Vendor/YourBundle/Entity/SuperEntityRepository.php
                  
                  public function findByCategoryAndOrderByName($category)
                  {
                      return $this->createQueryBuilder('e')
                          ->where('e.category = :category')
                          ->setParameter('category', $category)
                          ->orderBy('e.name', 'ASC')
                          ->getQuery()
                          ->getResult()
                      ;
                  }
                  

                  ...使排序变得非常容易.

                  ... makes sorting pretty easy.

                  希望有所帮助.

                  这篇关于“我们"一个 DoctrineCommonCollectionsArrayCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='maJS2'></small><noframes id='maJS2'>

                      <tfoot id='maJS2'></tfoot>

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

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