Symfony 1.2 Admin with custom primary key

April 27, 2009 – 10:40 pm

Playing with the Symfony 1.2 Admin Generator I noticed that “out-of-the-box” it only likes to play with integer primary keys (ideally based on auto-increment columns I assume). Now, there are cases where you want to use primary keys that aren’t neccessarily numeric, for example countries or languages, where you could use the corresponding ISO codes as id.

When trying this with the new admin generator, all looks fine initially (”list” action works, “new” action works), but when you try to edit a record, which happens automatically after creating one, you will get a “404 Page not found” error. Looking at the logs you’ll see that Symfony interprets the non-numeric id as action name, and matches it with the :module/:action route. Of course this action doesn’t exist, resulting in above-mentioned error.

What’s going on here? Turns out, the culprit is the object routing class that the admin generator uses for generated modules. In my case the responsible class is called “sfPropelRouteCollection”, and it seems to expect the id column to be numeric, judging by it’s default setting for the requirements:

\d+

In above expression the \d is regulatory speak for “a numeric character”, and the + means “one or more”. Luckily for us, all it takes is changing this requirement setting to make the generated modules work with non-numeric primary keys as well! The pattern that worked best for me is this:

[\d\w]+

Translated this means “either a numeric character or a letter character”, and “one or more” of those. Depending if you have special characters in your primary key (you really shouldn’t), you may have to massage this pattern a little, but take care to watch for “/” and “.” since those are used by Symfony’s routing to tokenize the url request.

The place to put this updated requirements is the routing.yml file, where it would look somewhat like this:

customer:
  class: sfPropelRouteCollection
  options:
    model:               Countries
    module:             countries
    prefix_path:    countries
    column:              id
    with_wildcard_routes: true
  requirements:  { id: "[\d\w]+" }

Watch out that the requirements entry is on the same level as class and options.

Needless to say I was very relieved when I found that this is less of a bug than a conventions issue, and that the admin generator didn’t completely break down on such a trivial requirement.

Happy custom Pk’ing!


Filed under: Symfony — Tags: — by Richtermeister

7 Comments »

  1. Pretty good post. I just stumbled upon your blog and wanted to say
    that I’ve really enjoyed reading your blog posts. In any case
    I’ll be subscribing to your feed and I hope you post again soon!

    Comment by Maria — June 23, 2009 @ 10:44 pm

  2. wow thanks for your snippet, i spent hour and days to find why the admin generator cannot work with id type string..
    now i know that we need to make and extra setting in routing.yml
    thanks once again..

    Comment by hendra1 — July 23, 2009 @ 7:35 pm

  3. Добрый день! jake@avtogazik.ru” rel=”nofollow”>……

    с ув….

    Trackback by Frankie — June 17, 2010 @ 10:59 am


  4. MedicamentSpot.com. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. Online Pharmacy. Order drugs online

    Buy:Acomplia.Zocor.Buspar.Prozac.Amoxicillin.Lipitor.Seroquel.Female Cialis.Lasix.Aricept.Ventolin.SleepWell.Wellbutrin SR.Lipothin.Benicar.Zetia.Female Pink Viagra.Nymphomax.Cozaar.Advair….

    Trackback by MILTON — July 21, 2010 @ 4:58 am

  5. cassette http://jvhsorstu5j.BABYCLOTHESNUT.INFO/tag/Mini+DV+cassette+VHS/ : DV…

    Mini…

    Trackback by cassette — August 29, 2010 @ 7:00 pm


  6. CheapTabletsOnline.Com. Canadian Health&Care.No prescription online pharmacy.Best quality drugs.Special Internet Prices. Low price drugs. Buy pills online

    Buy:Mega Hoodia.Petcam (Metacam) Oral Suspension.100% Pure Okinawan Coral Calcium.Valtrex.Prednisolone.Lumigan.Accutane.Zovirax.Actos.Prevacid.Arimidex.Zyban.Synthroid.Retin-A.Nexium.Human Growth Hormone….

    Trackback by GREGORY — September 5, 2010 @ 9:40 am


  7. CheapTabletsOnline.com. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices.Best quality drugs. Low price pills. Order drugs online

    Buy:Cozaar.Lasix.Amoxicillin.Nymphomax.Ventolin.Prozac.Aricept.Acomplia.Benicar.SleepWell.Wellbutrin SR.Female Pink Viagra.Zocor.Female Cialis.Buspar.Advair.Lipothin.Seroquel.Lipitor.Zetia….

    Trackback by CLAYTON — September 6, 2010 @ 6:00 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress