Yii Framework Forum: Same model used twice on same page - Yii Framework Forum

Jump to content

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

Same model used twice on same page Rate Topic: -----

#1 User is offline   Giordano 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 07-March 10

Posted 09 April 2010 - 04:55 PM

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
0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 09 April 2010 - 05:44 PM

If those two fields are the only fields in the form, tabular input might be a solution.
http://www.yiiframew...uide/form.table

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   got 2 doodle 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 248
  • Joined: 12-December 08
  • Location:Beautiful Nova Scotia, Canada

Posted 10 April 2010 - 09:21 AM

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
Check out myYii powered website
0

#4 User is offline   b3wii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 07-June 12

Posted 15 May 2013 - 06:53 AM

View PostGiordano, on 09 April 2010 - 04:55 PM, said:

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


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.yiiframew...-the-same-form/
0

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