Yii Framework Forum: Form helpers available for HAS_MANY and MANY_MANY? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Form helpers available for HAS_MANY and MANY_MANY? Rate Topic: -----

#1 User is offline   ZMan9854 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 23-March 10

Posted 04 May 2010 - 02:27 AM

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

Thank you!
0

#2 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 174
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 04 May 2010 - 02:57 AM

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.

Example:
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
0

#3 User is offline   ZMan9854 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 23-March 10

Posted 04 May 2010 - 03:56 AM

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?

Thanks!
0

#4 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 174
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 04 May 2010 - 05:12 PM

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

http://www.yiiframew...g-a-nested-form
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users