迭代原则集合由某个字段排序 [英] Iterate Doctrine Collection ordered by some field
本文介绍了迭代原则集合由某个字段排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要这样的:
$products = Products::getTable()->find(274);
foreach ($products->Categories->orderBy('title') as $category)
{
echo "{$category->title}<br />";
}
我知道是不可能的,但...像这样不创建Doctrine_Query?
I know is it not possible, but... How can I do something like this without creating a Doctrine_Query?
谢谢。
推荐答案
只是看着同样的问题。你需要将Doctrine_Collection转换为一个数组:
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
然后可以创建自定义排序函数并调用它: p>
Then you can create a custom sort function and call it:
// sort children
usort($children, array(__CLASS__, 'compareChildren')); // fixed __CLASS__
其中compareChildren看起来像:
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);
}
这篇关于迭代原则集合由某个字段排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文