many-to-many

I have many-to-many relation between Person and Item.

There are three tables / models: Person, Item, Person_Item.

I need a form "ADD PERSON". First part of this form simply contains person data (name, surname etc.).

After then there should be 10 drop downs. Every drop down should contain list of all available items in db.

So, after submitting form Person_Item table also should be populate with items from all those 10 drop downs if there are filled in (not empty).

I do not understand the last thing - how this should be handled via ActiveRecord prototype?

If there is only one drop down then the code should be the following (i guess i should use transactions instead of the following, but this doesn’t matter in this post):

  1. View



 echo CHtml::activeDropDownList($itemmodel, 'id',            //ID - WORKS ONLY FOR 1 ITEM, BUT IF THERE ARE 10?

              $item_list,

              array('empty' => t('(Select item)')));

   



2)Controller




if(isset($_POST['PersonModel']))

{

  $person->attributes=$_POST['PersonModel'];

  if ($person->save())

 {

    $id = getpersonid;

    $item_person = new ItemPersonModel();

    $item->attributes = $_POST['ItemModel']; //  THIS PROBABLY WORKS ONLY FOR ONE ITEM! BUT I HAVE 10 ITEMS! HOW TO LOOP / HOW TO CREATE 10 DROPDOWNS IN VEW


    $item_person->person_id = $id;

    $item_person->item_id = $item->id;

     

     if ($item_person->save())

     {

            // item and person is saved

     } 

 }

}



r.

Hi,

It sounds like you actually want to have one person model and multiple item models. What I suggest would be to pass an array of models for your items and then you could reference them within your POST object.

I had this same issue a few months back. Here was my post, there are two tutorials that you have to combine to achieve this. This is one of the tutorials: Two or more models. This is the other: Tabular Input

Hope this helps,

-Nazum