shorif2000
shorif2000

Reputation: 2654

yii multi records on multi form with multi models

I have a form with multiple models. 1 model will display 1 record and other models will display multiple records in tabular format. When i submit to save i only get the last record from each model. How can i get all the records of a model?

here is a sample of how I am rendering the tabular format model on the form

  // lines tab
    $lineListContent = "<div id='form_line_list'>
        <table>
        <tr>
        <th>JOBNO</th>
        <th>NAME</th>
        <th>SEQ</th>
        <th>DATEIN</th>
        <th>DATEDONE</th>
        <th>STATUS</th>
        <th>PCENthONE</th>
        <th>EARNED</th>
        <th>VALUE</th>
        <th>COMMENTS</th>
        <th>SENTBACK</th>
        <th>DATEDUE</th>
        <th>ORIGTAPES</th>
        </tr>
        ";
    $lineListContentRows = null;
    foreach ($jobs as $i=>$item){
        $lineListContentRows = $lineListContentRows . $this->renderPartial('/jobs/_form', array('model'=>$item,'form'=>$form),TRUE);
    }
    $lineListContent = $lineListContent . $lineListContentRows . "
                </table></div>";

The for loop grabs the input field for its model

_form

<?php
/* @var $this JobsController */
/* @var $model Jobs */
/* @var $form CActiveForm */
?>

<div class="form">


    <tr>
        <td><?php echo $form->textField($model,'JOBNO'); ?></td>
        <td><?php echo $form->textField($model,'NAME'); ?></td>
        <td><?php echo $form->textField($model,'SEQ'); ?></td>
        <td><?php echo $form->textField($model,'DATEIN'); ?></td>
        <td><?php echo $form->textField($model,'DATEDONE'); ?></td>
        <td><?php echo $form->textField($model,'STATUS'); ?></td>
        <td><?php echo $form->textField($model,'PCENTDONE'); ?></td>
        <td><?php echo $form->textField($model,'EARNED'); ?></td>
        <td><?php echo $form->textField($model,'VALUE'); ?></td>
        <td><?php echo $form->textField($model,'COMMENTS'); ?></td>
        <td><?php echo $form->textField($model,'SENTBACK'); ?></td>
        <td><?php echo $form->textField($model,'DATEDUE'); ?></td>
        <td><?php echo $form->textField($model,'ORIGTAPES'); ?></td>
    </tr>


</div><!-- form -->

Upvotes: 0

Views: 906

Answers (1)

dInGd0nG
dInGd0nG

Reputation: 4114

If you want to save tabular data you have to prefix the attributes with numbers in bracket. So

 foreach ($jobs as $i=>$item){
        $lineListContentRows = $lineListContentRows . $this->renderPartial('/jobs/_form', array('model'=>$item,'form'=>$form,'i'=>$i),TRUE);
    }

and in your _form.php change each textField's second argument as

<td><?php echo $form->textField($model,"[$i]JOBNO"); ?></td>// remember use double quotes

If you make the changes as above your $_POST['modelName'] will contain data as an array. Iterate it through a forloop and save each items. For more info check the official documentation.

Upvotes: 1

Related Questions