Iterate Doctrine Collection ordered by some field

后端 未结 4 1476
没有蜡笔的小新
没有蜡笔的小新 2021-02-19 16:34

I need something like this:

        $products = Products::getTable()->find(274);
        foreach ($products->Categories->orderBy(\'title\') as $category         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-19 17:01

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

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

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

提交回复
热议问题