Form choice filter auto sort by name

The following snippet will auto-sort all sfWidgetFormDoctrineChoice elements on symfony filters.

On BaseFormFilterDoctrine.class.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
abstract class BaseFormFilterDoctrine extends sfFormFilterDoctrine
{

    public function setup()
    {
        $guesses = array('name', 'title', 'description', 'subject', 'keywords', 'id');

        foreach ($this->getWidgetSchema()->getFields() as $f)
        {
            if ($f instanceof sfWidgetFormDoctrineChoice)
            {
                $model = $f->getOption('model');
                $table = Doctrine::getTable($model);
                $order_by = null;
                foreach ($guesses as $descriptionColumn)
                {
                    if ($table->hasColumn($descriptionColumn))
                    {
                        $order_by = $descriptionColumn;
                        break;
                    }
                }
               
                if (isset($order_by) && $order_by)
                {
                    $f->setOption('order_by', array($order_by, 'asc'));
                }
            }
           
        }

    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>