Iterate Doctrine Collection ordered by some field

inakiabt picture inakiabt · Oct 27, 2009 · Viewed 20.1k times · Source

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.

Answer

Max Gordon picture Max Gordon · Jul 22, 2010

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