V
V
Vyacheslav_Shilov2020-08-04 11:25:58
Yii
Vyacheslav_Shilov, 2020-08-04 11:25:58

Why is data not saved to the database in Yii?

I do everything according to the documentation.
https://www.yiiframework.com/doc/guide/1.1/en/form...
The data is saved in the post (top left on the screen), not in the database.
5f291bfe40d2f488495164.png

Controller code:

<?php

class Home5Controller extends Controller
{
  public function actionIndex()
  {
        $items = Post1::model()->findAllByAttributes(array('phone' => array(1)));
        //$items=$this->getItemsToUpdate();
        if(isset($_POST['Post1'])){
        foreach ($_POST['Post1'] as $post) {
            foreach ($post as $a)
                echo $a . "<br>";
        }

        }
        if(isset($_POST['Post1']))
        {
            $valid=true;
            foreach($items as $i=>$item)
            {
                if(isset($_POST['Post1'][$i]))
                    $item->attributes=$_POST['Post1'][$i] or die('mm');
                $valid=$item->validate() && $valid;
            }
            if($valid)
                echo 'ss';
    }
        // отображаем представление с формой для ввода табличных данных
        $this->render('index',array('items'=>$items));
    //$this->render('index');
  }
}


View code:
<?php
/* @var $this Home5Controller */

?>
<div class="form">
    <?php echo CHtml::beginForm(); ?>
    <table>
        <tr><th>Имя</th><th>Стоимость</th><th>Количество</th><th>Описание</th></tr>
        <?php foreach($items as $i=>$item): ?>
            <tr>
                <td><?php echo CHtml::activeTextField($item,"[$i]id"); ?></td>
                <td><?php echo CHtml::activeTextField($item,"[$i]name"); ?></td>
                <td><?php echo CHtml::activeTextField($item,"[$i]lastname"); ?></td>
                <td><?php echo CHtml::activeTextField($item,"[$i]phone"); ?></td>
            </tr>
        <?php endforeach; ?>
    </table>

    <?php echo CHtml::submitButton('Сохранить'); ?>
    <?php echo CHtml::endForm(); ?>
</div><!-- form -->

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question