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

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

                  上一篇:如何得到一维标量数组作为dql查询结果? 下一篇:加入子查询与学说 2 DBAL

                  相关文章

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

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