• <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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                  How to parse django style template tags(如何解析 django 样式模板标签)
                  What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                  How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                  What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                  php in background exec() function(php 后台 exec() 函数)

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