I need something like this:
$products = Products::getTable()->find(274);
foreach ($products->Categories->orderBy(\'title\') as $category
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:
foreach ($collection->getIterator()->asort() as $element) { /* ... */ }