Development

HowtoSortAdminGeneratorListByForeignTableName (diff)

You must first sign up to be able to contribute.

Changes between Version 5 and Version 6 of HowtoSortAdminGeneratorListByForeignTableName

Show
Ignore:
Author:
boutell (IP: 68.84.72.111)
Timestamp:
01/02/09 22:31:45 (9 years ago)
Comment:

Filter pulldown menus need sorting too

Legend:

Unmodified
Added
Removed
Modified
  • HowtoSortAdminGeneratorListByForeignTableName

    v5 v6  
    2525}}} 
    2626 
    27 For the most part, the admin generator does a good job with this. But sorting is a problem
     27For the most part, the admin generator does a good job with this. But sorting is a problem in two places: in the actual paginated list of records, and in the dropdown menus used to filter by studio, instructor, etc. I'll present solutions for both issues. The first required the creation of a plugin, but fortunately it's finished and available for you to use. The second simply required digging deeply into undocumented Symfony code to figure out the right way to enable the behavior via `generator.yml`
    2828 
    29 When the user clicks on the "Name" column, the list is sorted alphabetically by name. But when they  click on the "Venue," "Studio," or "Instructor" column, the list is sorted by foreign ID- an order that doesn't make much sense to human beings. 
     29== Alphabetically Sorting the Paginated List of Records by a Foreign Key == 
    3030 
    31 == The Easy Way: Use [http://www.symfony-project.org/plugins/sfAdminSortByForeignNamePlugin sfAdminSortByForeignNamePlugin] == 
     31When the user clicks on the "Name" column, the list is sorted alphabetically by name. But when they  click on the "Venue," "Studio," or "Instructor" column, the list is sorted by foreign ID- an order that doesn't make much sense to human beings (although it does usually result in sorting by order of creation, which is sometimes useful). There are two ways to fix this. 
    3232 
    33 How can we fix this? The easy way is to use my [http://www.symfony-project.org/plugins/sfAdminSortByForeignNamePlugin sfAdminSortByForeignNamePlugin]. Once you have installed that plugin, you can automatically add support for sorting by foreign table name fields to any admin generator class by adding just a few lines to `event/actions/actions.class.php`, like so: 
     33=== The Easy Way: Use [http://www.symfony-project.org/plugins/sfAdminSortByForeignNamePlugin sfAdminSortByForeignNamePlugin] === 
     34 
     35The easy fix is to use my [http://www.symfony-project.org/plugins/sfAdminSortByForeignNamePlugin sfAdminSortByForeignNamePlugin]. Once you have installed that plugin, you can automatically add support for sorting by foreign table name fields to any admin generator class by adding just a few lines to `event/actions/actions.class.php` (substitute your admin-generated class for `event`): 
    3436 
    3537{{{ 
    4446}}} 
    4547 
    46 Note that the name of the class being administered must be passed as the second argument to `addSortByForeignName`. 
     48Note that the name of the model class being administered must be passed as the second argument to `addSortByForeignName`. 
    4749 
    48 == How It Works == 
     50=== The Hard Way (How The Plugin Works) === 
    4951 
    5052That's all you really need to know. However, interested parties and those whose needs are similar but not identical to this scenario will want to read on to learn how this works. 
    122124An even better solution: migrate this code into a separate class where it can be reused by many admin generator subclasses. And that is what I've done with [http://www.symfony-project.org/plugins/sfAdminSortByForeignNamePlugin sfAdminSortByForeignNamePlugin].  
    123125 
    124 -Tom Boutell, http://www.boutell.com/ 
     126== Alphabetically Sorting the Filter Dropdown Menus by Foreign Key == 
     127 
     128I was happy to have a fix for the main problem, but eventually noticed a secondary issue as well. Symfony's admin generator allows us to easily filter the displayed records by any field, including foreign key fields. And it is smart enough to display the names of the foreign objects if the field is a foreign key. 
     129 
     130Unfortunately, however, there was no immediately obvious way to sort the list of filter options in the dropdown menu... alphabetically or otherwise. 
     131 
     132As it turns out, there is a supported way to fix this in `generator.yml`. It just isn't well-documented (or indeed mentioned at all in the admin generator chapter of the book, as far as I can tell, for either Symfony 1.0 or 1.2). 
     133 
     134All you have to do is write a static public function in your foreign model peer class that returns an array containing all of the objects of that class, in the order you want them. For instance, I use this method in my `BandPeer.class.php` file: 
     135 
     136{{{ 
     137  public static function getAll() 
     138  { 
     139    $c = new Criteria(); 
     140    $c->addAscendingOrderByColumn(self::NAME); 
     141    return self::doSelect($c); 
     142  } 
     143}}} 
     144 
     145Then specify this method via the `config/generator.yml` of your admin generated class. Here's how I do it for several different foreign key filters in the back end of [http://salsadelphia.com/ salsadelphia.com]: 
     146 
     147{{{ 
     148      fields: 
     149        venue_id: 
     150          params: 
     151            peer_method: getAll 
     152        studio_id: 
     153          params: 
     154            peer_method: getAll 
     155        instructor_id: 
     156          params: 
     157            peer_method: getAll 
     158        dj_id: 
     159          params: 
     160            peer_method: getAll 
     161        band_id: 
     162          params: 
     163            peer_method: getAll 
     164}}} 
     165 
     166Note that the use of a custom peer method means that you're not restricted to alphabetical sorting. In my case I sort first by whether the user has ownership of the studio, venue, etc. and then alphabetically. That helps users find the filters they are most likely to want more quickly. 
     167 
     168Don't forget to do a `symfony cc` before testing your work. 
     169 
     170-Tom Boutell, [http://www.boutell.com/ Boutell.Com] and [http://www.punkave.com/ P'unk Avenue]