Iteratively Posting Arrays To Database


I’m trying to create a form for the user to enter a bunch of measurements. The form may have 20 to 90 items for the user to enter depending on the type of project and it eventually needs to be dynamic. Currently I’m storing all the titles and values for each measurement into an array and then putting that array into the database inside the ‘measurements’ attribute. Whenever I hand code the form and use this in the controller:


everything is stored and retrieved properly from the database. What I want to do instead, though, is to create a foreach loop that automatically churns out all of the fields in the array so that the form can adapt to different sized arrays and so that I don’t have to hand code a 90 item form. The problem is that when I use the code below it only saves the very last value entered to the array and none of the others. When I then retrieve the array back from the database to update it, all the values in the form have been set to the last one rather than keeping the original values entered by the user.

I’m assuming I need to change something in the controller to post all the items of the array in a different manner, but have no idea how to do that.

I’m very new to Yii and PHP so please be thorough in your explanations. Thanks a bunch!

$i = 0;

        foreach($model->measurements as $a)


            echo '<div class="row">';

            echo $form->labelEx($model, $model->measurements[$i][0]);

            echo $form->hiddenField($model, 'measurements[$i][0]', array('value' => $model->measurements[$i][0]));

            echo $form->textField($model, 'measurements[$i][1]',array('size'=>20,'maxlength'=>20));

            echo $form->error($model, 'meaurements');

            echo '</div>'; 




you might consider making the measurements its own table with a one to many relationship with your current model. Are you familiar with a one to many table relationship in sql?