N
N
Nikolai Novosad2015-02-16 16:40:14
Yii
Nikolai Novosad, 2015-02-16 16:40:14

How to update the data in the database in yii through the form?

Hello.
There are 2 tables - the first one is a reference book of indicators: indicator_type (id, name). The second one is for storing indicator values ​​(id, indicator_type_id, value).
It is necessary through the form to update the data in the database. But I ran into a problem to display even the form.
The Indicator model was created through Gii.
In the controller I do this:

public function actionFilling()
    {        
        $model = Indicator::model()->findAll();
        $this->render('filling', array('model' => $model));
    }

In the view, I do this:
<div class="form">

    <?php
    $form = $this->beginWidget('CActiveForm');
    echo $form->errorSummary($model);
    foreach ($model as $type) {
        ?>
        <div class="row">
            <?php
            echo $form->hiddenField($model, '[' . $type->id . ']indicator_type_id');
            echo $form->textField($model, '[' . $type->id . ']value');
            ?>
        </div>
    <?php }
    ?>
    <div class="row buttons">
        <?php echo CHtml::submitButton('Отправить'); ?>
    </div>
    <?php $this->endWidget(); ?>
</div>

After all this I get: Fatal error: Call to a member function hasErrors() on a non-object in Z:\home\yii.loc\www\framework\web\helpers\CHtml.php on line
2452 subsequent update of the data in the database?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander N++, 2015-02-16
@sanchezzzhak

www.yiiframework.com/doc/guide/1.1/ru/form.view
The error is that you are giving an array of models to the form and you need 1 model
www.yiiframework.com/doc/guide/1.1/ru/database.ar

R
Ruslan, 2015-02-16
@rutrader

Perhaps the problem is that you are selecting across all elements instead of selecting just one element.
find()

O
Oleg Titarenko, 2015-02-17
@olegtytarenko2

I do not remember how in the first version, but in the second you can do this
and here is an excellent manual that describes how to do it correctly
www.yiiframework.com/doc/guide/1.1/ru/database.ar#sec-5

N
Nikolai Novosad, 2015-02-17
@nik_neman

Thanks everyone for the replies.
Managed to do this:
Controller

public function actionFilling()
    {
        $model = Indicator::model()->findAll();
        if (isset($_POST['Indicator'])) {
            foreach ($_POST['Indicator'] as $type_id => $items) {
                $i = Indicator::model()->findByAttributes(array('indicator_type_id' => $type_id));
                if ($i === null) {
                    $i = new Indicator;
                }
                $i->attributes = $items;
                $i->save();
            }
        }
        $this->render('filling', array('model' => $model));
    }

Performance:
<div class="form">
    <?php
    $form = $this->beginWidget('CActiveForm');
    echo $form->errorSummary($model);
    foreach ($model as $type) {
        ?>
        <div class="row">
            <?php
              echo $form->hiddenField($type, '[' . $type->id . ']indicator_type_id');
              echo $form->textField($type, '[' . $type->id . ']value');
            ?>
        </div>
    <?php }
    ?>
    <div class="row buttons">
        <?php echo CHtml::submitButton('Отправить'); ?>
    </div>
    <?php $this->endWidget(); ?>

</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question