I've got a symfony admin generated module for my Poem class. My poem table has a foreign_key constraint to my MasterIndex table (user_id). When using the Symfony admin generator, in list mode, it displays a drop-down filter for my user_id. I can change the data that is being displayed by modifying the __toString() method in my MasterIndex class to display the user name instead of the Id.

How can I sort the data that is being displayed in the filter drop-down?

I found this link which proposes a solution, but the peer_method that it uses only seems to work with Propel.

I'm using Doctrine 1.2 with my Symfony 1.4 project.

有帮助吗?

解决方案

You can find available options for the sfWidgetFormDoctrineChoice here

* model:        The model class (required)
* add_empty:    Whether to add a first empty value or not (false by default)
                If the option is not a Boolean, the value will be used as the text value
* method:       The method to use to display object values (__toString by default)
* key_method:   The method to use to display the object keys (getPrimaryKey by default)
* order_by:     An array composed of two fields:
                  * The column to order by the results (must be in the PhpName format)
                  * asc or desc
* query:        A query to use when retrieving objects
* multiple:     true if the select tag must allow multiple selections
* table_method: A method to return either a query, collection or single object

You can use the order_by option like this: ...->setOption('order_by', array('some_field', 'asc')).

UPDATE:

If you want to order by multiple fields you can write a custom query and add multiple order by to it and then use the query or table_method option instead of order_by.

E.g. (you should write the query into a table class instead):

$query = Doctrine_Core::getTable('UserTable')->createQuery('u')->orderBy('u.last_name asc, u.first_name asc');
$this->getWidget('user_id')->setOption('query', $query);
// if you add some where condition to the query
// don't forget to set it to the validator as well
$this->getValidator('user_id')->setOption('query', $query);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top