test.ical.ly | getting the web by the balls

Oct/10

19

Some helping hands when working with embedded doctrine relations in symfony forms

Many of you will have been confronted with the symfony form sub-framework at some point. And when you had to work with model relations that were a tad more complicated that the examples from the documentation you will have experiences difficulties. I know I have. There are however three interesting plugins that can be of great help.

Let me start with the most simple one of them.

sfAdminHijackPlugin

(http://github.com/caefer/sfAdminHijackPlugin)

This is one my own plugins and provides a single and very simple functionality for one-to-many relations. It provides a widget that you can use on the column that contains the foreign key.

You will then get a JQuery powered widget that lets you choose from existing objects to be related using autocomplete search. You can delete a relation and you can add one on the fly with a JQuery modal dialogue.

See my older post for more detailed information.

ahDoctrineEasyEmbeddedRelationsPlugin

(http://www.symfony-project.org/plugins/ahDoctrineEasyEmbeddedRelationsPlugin)

This plugin provides much more functionality that the first plugin. To use it your forms have to inherit from a form class included in the plugin. It overloads the embedRelation() method which you can use to add, change and delete relations.

It will change your form markup to include a UI for all these interactions. This plugin is highly configurable which unfortunately can result in misconfiguration and spending a few moments to figure out the correct options for your requirement.

Once set up though it will work like a charm.

Update: this is no longer recommended. See comments for details!

sfDoctrineDynamicFormRelationsPlugin

(http://github.com/kriswallsmith/sfDoctrineDynamicFormRelationsPlugin)

The last plugin is my personal favourite as it does not force you to change your inheritance chain or to accept form markup. It only takes care about the backend for handling adding, changing and deleting relations from the form data send in a request. The rest is fully up to you. You can use your favourite Javascript library or homegrown functionality to make dynamic changes to the form markup and the plugin will take care of handling the additional data.

This plugin provides a new method embedDynamicRelation() which takes care of the backend part of the form only. You can use Javascript to handle the frontend (extending, reducing the form). This is what I use in the page composition feature.

See also Kris Wallsmith’ talk on symfony day 2010 (slides 86-103) for more info.

I hope this saves somebody a few headaches. ;)

· · ·



  • http://symfony-world.blogspot.com Tomasz Ducin

    Thanks for this summary. There are so many symfony plugins that it can be really painful to find what you need (and lots of things has already been done so far). I wish that there’d be a standard solution for embedding forms in symfony 2, being as easy to understand and use as admin-module generator.yml or schema.yml.

  • http://test.ical.ly Christian

    @Tomasz true. Although with the architecture of the forms you will always have to edit the form classes themselves. Using sfDoctrineDynamicFormRelationsPlugin this is far less painful.

  • http://test.ical.ly Christian

    Daniel Lohse (the developer of ahDoctrineEasyEmbeddedRelationsPlugin contacted me on Twitter today to say that he recommends to use sfDoctrineDynamicFormRelationsPlugin over ahDoctrineEasyEmbeddedRelationsPlugin.

    See here
    and here

  • http://sf.khepin.com Mr. K

    Well sfDoctrineDynamicFormRelationsPlugin seems nice if you’re able to use it!
    It just lacks an example (a complete one). Like … what is the form I send through ajax supposed to look like etc…
    Been unable to use it so far, but would be happy to publish a complete example myself if I ever understand how to make it work!

  • http://test.ical.ly Christian

    @Mr.K I actually plan do write down an example but due to time issues this will have to wait a few days..

  • http://sf.khepin.com Mr. K

    Well, in the end it’s not THAT difficult and after some hacking around I finally found my way.
    The way I did it is here: http://sf.khepin.com/2010/10/easy-ajax-forms-with-sfdoctrinedynamicformrelationsplugin/
    Will be interested to know if I missed some stuff!

  • Pingback: Tweets that mention • Some helping hands when working with embedded doctrine relations in symfony forms | test.ical.ly -- Topsy.com

  • http://ardsoluciones.com.ar/blog Nicolas Ardison

    Wow man, you rocks, this post have the things that i have been looking for :).
    Thanks =).

    I will comment it in my blog in spanish.

  • luckyluck

    Found here a weird post with a working different approach. I tried to use it with a particularly complicated form and it seems be helping

    http://arialdomartini.wordpress.com/2011/04/01/how-to-kill-symfony%E2%80%99s-forms-and-live-well/

<<

>>

Theme Design by devolux.nh2.me