13

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?

Thanks.

Rauni Lillemets
  • 2,299
  • 1
  • 26
  • 39
inakiabt
  • 1,953
  • 1
  • 16
  • 28

4 Answers4

31

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
Max Gordon
  • 311
  • 3
  • 2
  • 5
    If the sorting is "permanent", using this way is far better than using Chris William's one. – avetisk Dec 25 '10 at 09:03
  • 2
    The downside is the permanence. Adding an orderBy to EVERY query for this relation will impact performance. – Mike Purcell Mar 28 '12 at 03:54
  • or by `@OrderBy` annotation : http://docs.doctrine-project.org/en/2.0.x/reference/annotations-reference.html#annref-orderby – zizoujab May 28 '13 at 21:44
9

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);
}
scube
  • 1,931
  • 15
  • 21
Chris Williams
  • 11,647
  • 15
  • 60
  • 97
  • 2
    your solution only worked when i changed it to: usort($children, array(_____CLASS_____, 'compareChildren')); – stoefln Jun 15 '11 at 07:41
9

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) { /* ... */ }
temochka
  • 436
  • 3
  • 5
4

You might add a sort function to Colletion.php :

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

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

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() );
    }

}
jantimon
  • 36,840
  • 23
  • 122
  • 185