V
V
Victor Umansky2017-03-12 01:32:33
Yii
Victor Umansky, 2017-03-12 01:32:33

Yii2, how to process an array of data from a post form to save to the database?

Good night everybody!
I decompose foreach, and it turns out that for each day there is a time and a break field($model, '[$k]work_start'), but I can't figure out how to get the array itself to save it to the database [$k]work_start - Mon|09:00, I accept in the controller and the updateProfile itself occurs in the model.
in VIEW there is a form,
this is an example, not all fields are completely here!

<?php foreach ($weeks as $k => $v) : ?>
        <?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, "[$k]work_start")->widget(TimePicker::classname(),
                            [
                                'value' => '00:00',
                                'pluginOptions' => [
                                    'showMeridian' => false,
                                ]
                            ])->label('');
                        ?>

<?= $form->field($model, "[$k]break_start")->widget(TimePicker::classname(),
                                [
                                    'value' => '00:00',
                                    'pluginOptions' => [
                                        'showMeridian' => false,
                                    ]
                                ])->label('');
                            ?>

        <?php $form = ActiveForm::end() ?>
    <?php endforeach; ?>
</table>

Model + I have a connection with tables.
There is such a work_schedule table, there are such fields (table at the bottom), from the form I receive data, they must be written to work_schedule, and the id from work_schedule must be written to the profile -> schedule_id table. And I can't figure out how to process the form itself to accept Days of the week and time???
db example
'id' => 1,
'week' => 'ПН',
'start_time' => '08.00',
'end_time' => '17.00',
'break_start' => '12.00',
'break_start' => '13.00',
is_weekend' => false,

class Profile extends ActiveRecord
{
    public $status_week;
    public $times;
    public $has_break;
    public $work_start;
    public $work_end;
    public $break_start;
    public $break_end;
    public $gallery;

//.........................

    public function updateProfile()
    {
        $id = Yii::$app->user->id;
        $profile = ($profile = Profile::findOne(['user_id' => $id])) ? $profile : new Profile();

        $profile->user_id = Yii::$app->user->id;

        if ($this->validate()) {
            foreach ($this->gallery as $gallery) {
                $hasObjectFile = new HasObjectFile();

                $hasObjectFile->title = $gallery->baseName . '.' . $gallery->extension;
                $hasObjectFile->label = md5(microtime()) . '.' . $gallery->extension;
                $hasObjectFile->profile_id = $profile->id;
                $hasObjectFile->type = HasObjectFile::FILE_TYPE_GALLERY;

                $hasObjectFile->save();
                $gallery->saveAs('upload/gallery/' . $hasObjectFile->label);
                $profile->gallery_id = $hasObjectFile->profile_id;
                $profile->save(false);
            }
            return true;
        }

 //$work = new WorkSchedule();

        if (is_array($this->times) && count($this->times)) {
            foreach ($this->times as $day => $values) {
                VarDumper::dump($values,11,1);die;
            }
        }

       
        return $profile->save() ? true : false;
    }

}

Controller
public function actionProfile()
    {
        $id = Yii::$app->user->id;
        $model = ($model = Profile::findOne(['user_id' => $id])) ? $model : new Profile();
        $weeks = Profile::weeks();
        $workGraph = WorkSchedule::workGraph();
        $gallery = Profile::getGallery();

        if ($model->load(Yii::$app->request->post())) {
            $model->gallery = UploadedFile::getInstances($model, 'gallery');
            if ($model->updateProfile()) {
                Yii::$app->session->setFlash('success', 'Профиль изменен!');
                return $this->refresh();
            } else {
                Yii::$app->session->setFlash('error', 'Профиль не изменен!');
                Yii::error('Ошибка записи. Профиль не изменен!');
                return $this->refresh();
            }
        }

        return $this->render('profile', [
            'model' => $model,
            'weeks' => $weeks,
            'workGraph' => $workGraph,
            'gallery' => $gallery,
        ]);
    }

I do var_dump and get such an array
[
    '_csrf' => 'ajI4RHc0RUkeVgABH3kPej9CVSAaTXYgBn0VLTVlIiAhA3ocO2EfCw=='
    'Profile' => [
        'status_week' => '1'
        ПН => [
            'work_start' => '09:00'
            'work_end' => '18:00'
            'break_start' => '12:00'
            'break_end' => '13:00'
        ]
        'has_break' => '1'
    ]
]

DB itself
'id' => 1,
'week' => 'ПН',
'start_time' => '08.00',
'end_time' => '17.00',
'break_start' => '12.00',
'break_start' => '13.00',
is_weekend' => false,

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Serenkov, 2017-03-12
@Uman

In the view, it's better to do this

<?php foreach ($weeks as $k => $v) : ?>
        <?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, "times[$k][work_start]")->widget(TimePicker::classname(),
                            [
                                'value' => '00:00',
                                'pluginOptions' => [
                                    'showMeridian' => false,
                                ]
                            ])->label('');
                        ?>

<?= $form->field($model, "times[$k][break_start]")->widget(TimePicker::classname(),
                                [
                                    'value' => '00:00',
                                    'pluginOptions' => [
                                        'showMeridian' => false,
                                    ]
                                ])->label('');
                            ?>

        <?php $form = ActiveForm::end() ?>
    <?php endforeach; ?>
</table>

Add the $times property to the model
class Profile extends ActiveRecord
{
    public $times;
    // .......
}

In controller action
if ($model->load(Yii::$app->request->post())) {
    // .....
    if (is_array($model->times) && count($model->times)) {
        foreach ($model->times as $day => $values) {
            // .... проверяете, сохраняете в базу
        }
    }
    // ......
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question