【问题标题】:Iterate Doctrine Collection ordered by some field迭代某些领域排序的学说集合
【发布时间】:2010-12-10 14:45:10
【问题描述】:

我需要这样的东西:

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

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

谢谢。

【问题讨论】:

    标签: php sorting collections doctrine doctrine-1.2


    【解决方案1】:

    你也可以这样做:

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

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

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

    【讨论】:

    • 如果排序是“永久的”,使用这种方式比使用克里斯威廉的方式要好得多。
    • 缺点是持久性。将 orderBy 添加到此关系的每个查询都会影响性能。
    【解决方案2】:

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

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

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

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

    compareChildren 看起来像这样:

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

    【讨论】:

    • 您的解决方案仅在我将其更改为时才有效:usort($children, array(_____CLASS_____, 'compareChildren'));
    【解决方案3】:

    你可以使用集合迭代器:

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

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

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

    【讨论】:

    • 尝试使用 $collection-&gt;getIterator()-&gt;asort() 但它只是返回 bool。
    • 对不起,我忘记了它是如何工作的。你是对的,它在成功时返回 true,在失败时返回 false。这么好的设计。
    • 如果您在迭代之前调用 asort,您将能够遍历已排序的列表。
    【解决方案4】:

    你可以在 Colletion.php 中添加一个排序函数:

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

    按年龄对用户的 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() );
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 2019-07-08
      • 1970-01-01
      相关资源
      最近更新 更多