2 objects of same model in one form

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

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 :frowning:

Take a look here:

http://www.yiiframework.com/doc/guide/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!