Yii Framework Forum: 2 objects of same model in one form - Yii Framework Forum

Jump to content

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

2 objects of same model in one form How to add 2 objects of the same model in one form Rate Topic: -----

#1 User is offline   Jan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 19-August 09

Posted 31 August 2009 - 07:59 AM

Not shure if its the right place to post this but couldnt find a better place :-)

I just started using yii and it's a great experiance so far.
However I stumbled upon a problem that I cant seem to fix:

I have a User model which has 2 FK to a Adress table (adress1 and adress2 which both store a adressId from the Adress table).
My relations in User are therefore:
         'adress1'=>array(self::BELONGS_TO, 'Adress', 'Id'),
      	 'adress2'=>array(self::BELONGS_TO, 'Adress', 'Id'),




In the Create and Update form of my user I want to add 2 blocks in which users can fill in the 2 adress.

I tried to do it like this:
<?php   $work = $user->adress1; ?>
<?php echo CHtml::activeLabelEx($work,'street'); ?>
<?php echo CHtml::activeTextField($work,'street',array('size'=>60,'maxlength'=>150)); ?>
</div>
<div class="simple">
<?php echo CHtml::activeLabelEx($work,'number'); ?>
<?php echo CHtml::activeTextField($work,'number',array('size'=>4,'maxlength'=>4)); ?>
</div>
...
<?php   $home = $user->adress2; ?>
<?php echo CHtml::activeLabelEx($home,'street'); ?>
<?php echo CHtml::activeTextField($home,'street',array('size'=>60,'maxlength'=>150)); ?>
</div>
<div class="simple">
<?php echo CHtml::activeLabelEx($home,'number'); ?>
<?php echo CHtml::activeTextField($home,'number',array('size'=>4,'maxlength'=>4)); ?>
</div>
...



However since both $work and $home are Adresses the Html generetad generates for both blocks fields with exactly the same IDs (e.g. Adress_Street).
I have therefore no way to seperate the two adresses
(If I do $adressX = $_POST['Adress'] I dont know which of the 2 adresses is in $_POST['Adress'] nor have I any means to access the second adress).

I'm shure there is a very simple solution for this, but I just cant seem to find it :-(
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 31 August 2009 - 08:10 AM

Take a look here:
http://www.yiiframew...uide/form.table

You could try a similar approach and either use attribute names like 'street[0]' and 'street[1]'in your two forms or even try 'street[work]' and 'street[home]'. Of course you need to name the other text fields respectively.
0

#3 User is offline   Jan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 19-August 09

Posted 31 August 2009 - 01:40 PM

View PostMike, on 31 August 2009 - 08:10 AM, said:

Take a look here:
http://www.yiiframew...uide/form.table

You could try a similar approach and either use attribute names like 'street[0]' and 'street[1]'in your two forms or even try 'street[work]' and 'street[home]'. Of course you need to name the other text fields respectively.

Thx, that is exactly what I needed!
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