我想保存表单中的多条记录


i want to save multi records from a form

我正在尝试从表单中保存批记录并保存到数据库中。我从高级示例中得到了一个示例,它是一个小部件。这示例只添加记录而不保存它我在小部件下面添加了一个保存底部,并在小部件的顶部表单。添加添加或更新的代码,我在小部件中添加id文件。从数据库中读取的记录。当我更新记录时,我没有任何问题,但当我添加记录时,程序不保存新记录,我跟踪程序,并看到$_post[model]包括旧记录,没有新记录。当我从中元素id字段时。所有记录重新保存我离开程序

pic1 pic2

_form_batch.php

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'person-form',
    'enableAjaxValidation'=>true,
    'enableClientValidation' => true,
)); ?>
<h2><?php 
$persons2=Person::model()->findAll();
echo Help::item('annotation','title',!Yii::app()->user->isGuest); ?></h2>
<div class="large-text"><?php echo Help::item('annotation','content'); ?></div>sas
<?php $this->widget('ext.widgets.tabularinput.XTabularInput',array(
        'models'=>$persons2,
        'containerTagName'=>'table',
        'headerTagName'=>'thead',
        'header'=>'
            <tr>
                                <td>'.CHtml::activeLabelEX(Person::model(),'id').'</td>
                <td>'.CHtml::activeLabelEX(Person::model(),'firstname').'</td>
                <td>'.CHtml::activeLabelEX(Person::model(),'lastname').'</td>
                                <td>'.CHtml::activeLabelEX(Person::model(),'country_id').'</td>    
                <td>'.CHtml::activeLabelEX(Person::model(),'eyecolor_code').'</td>
                                <td>'.CHtml::activeLabelEX(Person::model(),'email').'</td>
                <td></td>
            </tr>
        ',
        'inputContainerTagName'=>'tbody',
        'inputTagName'=>'tr',
        'inputView'=>'extensions/_tabularInputAsTable',
        'inputUrl'=>$this->createUrl('request/addTabularInputsAsTable2'),
        'addTemplate'=>'<tbody><tr><td colspan="6">{link}</td></tr></tbody>',
        'addLabel'=>Yii::t('ui','Add new row'),
        'addHtmlOptions'=>array('class'=>'blue pill full-width'),
        'removeTemplate'=>'<td>{link}</td>',
        'removeLabel'=>Yii::t('ui','Delete'),
        'removeHtmlOptions'=>array('class'=>'red pill'),
    )); ?>
    <div class="action">
        <?php echo CHtml::submitButton($model->isNewRecord ? Yii::t('ui', 'Create') : Yii::t('ui','Save'),array('class'=>'btn btn-primary')); ?>
        <?php echo CHtml::link(Yii::t('ui', 'Cancel'), $model->isNewRecord ? array('admin') : $this->getReturnUrl(), array('class'=>'btn')) ?>
    </div>
<?php $this->endWidget(); ?>

控制器:

      $person = new Person;
        $persons = $this->getItemsToUpdate();
        //  print_r($_POST); exit;
        if (isset($_POST['Person'])) {
            $valid = true;
            foreach ($persons as $i => $person) {
                if (isset($_POST['Person'][$i]))
                $person->attributes = $_POST['Person'][$i];
                $valid = $person->validate() && $valid;
                print_r($_POST['Person'][$i]);
                $person->save();
                $errores = $person->getErrors();
               // print_r($errores);
               // echo " valid: " . $valid . "<br>";
            }
        }
        $this->render('hame', array(
            'model' => $persons,
        ));
    }
    public function getItemsToUpdate() {
        // Create an empty list of records
        $persons = array();
print_r($_POST['Person']);
        // Iterate over each item from the submitted form
        if (isset($_POST['Person']) && is_array($_POST['Person'])) {
            foreach ($_POST['Person'] as $person) {
                // If item id is available, read the record from database 
                if (array_key_exists('id', $person)) {
                    $persons[] = Person::model()->findByPk($person['id']);
                }
                // Otherwise create a new record
                else {
                    $persons[] = new Person();
                }
            }
        }
        return $persons;
    }

第一点是:

**$person** = new Person;
foreach ($persons as $i => **$person**) {

第二个是:

我在foreach和save方面遇到了一些问题,最好使用

$cmd = Yii::app()->db->createCommand();
$cmd->insert(...
$cmd->update(...

否则,您必须在每次迭代中创建新模型,然后使用save()