Form helpers available for HAS_MANY and MANY_MANY?
Posted 04 May 2010 - 02:27 AM
One thing I cannot figure out how to do, or find much information on is how to setup these relations in a form that allows easy updates of this information without calculated fields and lots of custom SQL. Create forms are simple enough with the listData() method or a static array. But how should one setup an update form?
Here's an example scenario. Say I have a Genre model and an Artist model. An Artist can HAVE_MANY Genres and a Genre can HAVE_MANY Artist. Therefore, the relation between these models is MANY_MANY. So say I'm editing a Song and I want to have a dropdown or something similar to select genres. So far, I can create a dropdown that lists genres, but I cannot figure out how to:
- Have the Genres saves to the Song without custom SQL
- Have saved Genres show up when I go to edit the Song again
- Make sure Genres that are de-selected are deleted from the Song
Is this a feature that is offered with Yii?
I've seen the extension available here (http://www.yiiframew...ncedarbehavior/), but this seems to provide strictly programmatic relationship-assignment features, but the issues I'm facing have all to do with the lack of form helpers between two related objects.
So far, the way I've accomplished this is by making a faux-field for each relationship (for instance, "genre_field", so it doesn't interfere with the AR relationship "genres"") which has both a custom calculated getter and setter that executes SQL deletes and inserts. Is this how it should be done?
If there is indeed a better way to do this, I think there should be a guide right in smack on the front of the documentation - I would be more than willing to write this since it has to be a very important part of many sites : )
Posted 04 May 2010 - 02:57 AM
Table/Model: Artist has one_many relationship with ArtistGenre
Table/Model: Genre has one_many relationship with ArtistGenre
Table/Model: ArtistGenre (with fields Artist and Genre which are foreign keys to the Artist and Genre models).
In your artist form you have to have a subform which updates the ArtistGenre Model:
...Artist specific fields...
......Dropdownlist with all genre's (Selected through the foreignkey ArtistGenre.Genre)
Posted 04 May 2010 - 03:56 AM