Question

I am trying save batch record from form and save to database. I got sample from Advanced Example it is a widgets. this. example only add record without save it I add a save bottom under widget and form top of widget. add code for add or update ,I add id filed in widget . records read from database . when I update record i have not any problem but when I add record my program don't save new record i trace program and see $_post[model] include old record and have not new record . when I element id field from from . all record re-save I left program

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(); ?>

controller:

      $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;
    }

No correct solution

OTHER TIPS

first point is:

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

second is:

i had some problems with foreach and save, better use

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

otherwise you have to create new model on each iteration and than use save()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top