Form helpers available for HAS_MANY and MANY_MANY?

I’m working on an application where there are many HAS_MANY or MANY_MANY relationships where one model connects to another model either directly between two tables or through a mapping table. I’ve successfully setup the relations between them and this has gone smoothly (i.e. $my_song->genres works great).

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 (, 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 : )

Thank you!

If you want to connect 2 tables that have a many_many relationship, you should create that extra table to store all connections. That way you’re dealing with a has_many (or belongs_to) relationship, again.


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:

Form Artist

…Artist specific fields…

…Form ArtistGenre

…Dropdownlist with all genre’s (Selected through the foreignkey ArtistGenre.Genre)

…EndForm ArtistGenre

EndForm Artist

Thanks for your reply - this sounds like a good start toward solving my issues. Would you mind giving an example of the subform code? Specifically, I’d like to include a dropdown list of genres in the song update form - is this something that can be done with ActiveForm methods, or would it require some more customized code?


Please read this article about nested forms or sub-forms: