Same model used twice on same page

Hi all,

I have the situation where I want the user to be able to select two categories for some advertisement he’s entering.

So in the controller I create two models for this association class adcatassoc, linking the category to the advertisement:




$adcatassoc1 = new Adcatassoc();

$adcatassoc2 = new Adcatassoc();

Both are passed to the view as $acaModel1 and $acaModel2 respectively, where they’re used to render two dropdownlists:




<div class="row">

    <?php echo CHtml::activeLabelEx($acaModel1,'adcategory_id'); ?>

    <?php echo CHtml::activeDropDownList($acaModel1,'adcategory_id', 

	CHtml::listData( $categoryModel->findAll(), 'id', 'title' ), array('prompt'=>'Kies een rubriek...') ); ?>

</div>


<div class="row">

      <?php echo CHtml::activeLabelEx($acaModel2,'adcategory_id'); ?>

      <?php echo CHtml::activeDropDownList($acaModel2,'adcategory_id', 

	CHtml::listData( $categoryModel->findAll(), 'id', 'title' ), array('prompt'=>'Kies een rubriek...') ); ?>

</div>

Thing is that both items generate exactly the same html:




<div class="row">

	<label for="Adcatassoc_adcategory_id" class="required">Rubriek <span class="required">*</span></label>		

     <select name="Adcatassoc[adcategory_id]" id="Adcatassoc_adcategory_id">

        <option value="">Kies een rubriek...</option>

        <option value="1">Reiki</option>

        <option value="2">Yoga</option>

     </select>

</div>


<div class="row">

	<label for="Adcatassoc_adcategory_id" class="required">Rubriek <span class="required">*</span></label>	

      <select name="Adcatassoc[adcategory_id]" id="Adcatassoc_adcategory_id">

         <option value="">Kies een rubriek...</option>

         <option value="1">Reiki</option>

         <option value="2">Yoga</option>

      </select>

</div>

Needless to say that saving these models does not work. But more importantly I’m curious to how you people would approach this situation.

Cheers

If those two fields are the only fields in the form, tabular input might be a solution.

http://www.yiiframework.com/doc/guide/form.table

/Tommy

This is a little hard to address without knowing your database structure. It looks like you have a table that holds ad details and it links to a table of ad categories. It isn’t logical that you could use the same field to address two categories.

So you either need a join table or two fields ad_category1 and ad_category2. So let us know a bit more about the data side of your app and maybe someone can steer you in a good direction.

I did something similar with programs and themes, one program belongs to one or more themes. I used a join table and a checkboxlist but I had to do the data manipulation in the controller.

doodle

You name you input fields like this:




<div class="row">

    <?php echo CHtml::activeLabelEx($acaModel1,'adcategory_id'); ?>

    <?php echo CHtml::activeDropDownList($acaModel1,'[1]adcategory_id', 

	CHtml::listData( $categoryModel->findAll(), 'id', 'title' ), array('prompt'=>'Kies een rubriek...') ); ?>

</div>


<div class="row">

      <?php echo CHtml::activeLabelEx($acaModel2,'adcategory_id'); ?>

      <?php echo CHtml::activeDropDownList($acaModel2,'[2]adcategory_id', 

	CHtml::listData( $categoryModel->findAll(), 'id', 'title' ), array('prompt'=>'Kies een rubriek...') ); ?>

</div>


and in Controller you use:


        $adcatassoc1->attributes=$_POST['Adcatassoc'][1];

        $adcatassoc2->attributes=$_POST['Adcatassoc'][2];




…and it should work.

Found here: http://www.yiiframework.com/wiki/362/how-to-use-multiple-instances-of-the-same-model-in-the-same-form/