php 迭代按某些字段排序的 Doctrine Collection

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1632922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:20:11  来源:igfitidea点击:

Iterate Doctrine Collection ordered by some field

phpsortingcollectionsdoctrinedoctrine-1.2

提问by inakiabt

I need something like this:

我需要这样的东西:

        $products = Products::getTable()->find(274);
        foreach ($products->Categories->orderBy('title') as $category)
        {
            echo "{$category->title}<br />";
        }

I know is it not possible, but... How can I do something like this without creating a Doctrine_Query?

我知道这是不可能的,但是......我怎么能在不创建 Doctrine_Query 的情况下做这样的事情?

Thanks.

谢谢。

采纳答案by Chris Williams

I was just looking at the same problem. You need to convert the Doctrine_Collection into an array:

我只是在看同样的问题。您需要将 Doctrine_Collection 转换为数组:

$someDbObject = Doctrine_Query::create()...;
$children = $someDbObject->Children;
$children = $children->getData(); // convert from Doctrine_Collection to array

Then you can create a custom sort function and call it:

然后你可以创建一个自定义排序函数并调用它:

// sort children
usort($children, array(__CLASS__, 'compareChildren')); // fixed __CLASS__

Where compareChildren looks something like:

compareChildren 看起来像这样:

private static function compareChildren($a, $b) {
   // in this case "label" is the name of the database column
   return strcmp($a->label, $b->label);
}

回答by Max Gordon

You can also do:

你也可以这样做:

$this->hasMany('Category as Categories', array(...
             'orderBy' => 'title ASC'));

In your schema file it looks like:

在您的架构文件中,它看起来像:

  Relations:
    Categories:
      class: Category
      ....
      orderBy: title ASC

回答by temochka

You could use collection iterator:

您可以使用集合迭代器:

$collection = Table::getInstance()->findAll();

$iter = $collection->getIterator();
$iter->uasort(function($a, $b) {
  $name_a = (int)$a->getName();
  $name_b = (int)$b->getName();

  return $name_a == $name_b ? 0 : $name_a > $name_b ? 1 : - 1;
});        

foreach ($iter as $element) {
  // ... Now you could iterate sorted collection
}

If you want to sort collection using __toString method, it will be much easier:

如果你想使用 __toString 方法对集合进行排序,它会容易得多:

foreach ($collection->getIterator()->asort() as $element) { /* ... */ }

回答by jantimon

You might add a sort function to Colletion.php :

您可以向 Colletion.php 添加一个排序函数:

public function sortBy( $sortFunction )
{
    usort($this->data, $sortFunction);
}  

Sorting a Doctrine_Collection of users by their age would look like this:

按年龄对 Doctrine_Collection 用户进行排序将如下所示:

class ExampleClass
{

    public static function sortByAge( $a , $b )
    {
         $age_a = $a->age;
         $age_b = $b->age;

         return $age_a == $age_b ? 0 : $age_a > $age_b ? 1 : - 1;
    }    

    public function sortExample()
    {
         $users = User::getTable()->findAll();
         $users ->sortBy('ExampleClass::sortByAge');

         echo "Oldest User:";
         var_dump ( $users->end() );
    }

}