Answer the question
In order to leave comments, you need to log in
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));
}
<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>
Answer the question
In order to leave comments, you need to log in
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
Perhaps the problem is that you are selecting across all elements instead of selecting just one element.
find()
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
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));
}
<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 questionAsk a Question
731 491 924 answers to any question