简体   繁体   中英

how to create models iteratively?

as a beginner in Yii i have a question that I want to create models in iteration. For example I want to create result model for 30 students. How to create these models in iteration? I cant hardcode becaue number students may vary from class to class. In this approach i need to use tabular input but for that purpose i need to bind each model`s attribute to some element like

textArea

Definitive guide for yii presents this code

<div class="form">
<?php echo CHtml::beginForm(); ?>
<table>
<tr><th>Name</th><th>Price</th><th>Count</th><th>Description</th></tr>
<?php foreach($items as $i=>$item): ?>
<tr>
<td><?php echo CHtml::activeTextField($item,"[$i]name"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]price"); ?></td>
<td><?php echo CHtml::activeTextField($item,"[$i]count"); ?></td>
<td><?php echo CHtml::activeTextArea($item,"[$i]description"); ?></td>
</tr>
<?php endforeach; ?>
</table>

<?php echo CHtml::submitButton('Save'); ?>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->

But how can i populate my array with distinct model names like model1,model2,model3 and so on, in iteration? thanks EDIT:

public function actionCreate($exam=1,$class=1,$subject=4,$max=5,$min=4)
    {
                $students=  Student::model()->findAllByAttributes(array('class_grade_id'=>$class));
                //CVarDumper::Dump($students,100,true);
               // die();
                $i=0;
                $data=array();
        $model=new SubjectResult;
                foreach($students as $key)
                    {
                    $data[$i]=new SubjectResult();
                    $i++;
                    }
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['SubjectResult']))
        {

            $model->attributes=$_POST['SubjectResult'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('create',array(
            'model'=>$model,
                        'data'=>$data,
        ));
    }

Add a hidden field array to keep the ids.

<?php foreach($items as $i=>$item): ?>
    <tr>
    <td><?php echo CHtml::activeTextField($item,"[$i]name"); ?>
    <?php echo CHtml::hiddenField('stud_ids[]' , $i);?>
    </td>
    <td><?php echo CHtml::activeTextField($item,"[$i]price"); ?></td>
    <td><?php echo CHtml::activeTextField($item,"[$i]count"); ?></td>
    <td><?php echo CHtml::activeTextArea($item,"[$i]description"); ?></td>
    </tr>
    <?php endforeach; ?>

In your update action,

$ids = $_POST["stud_ids"]

Pass this $ids to foreach to get each id and load model with id then update the records.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM