K
K
Konstantin Zhikhor2018-08-20 14:08:15
Yii
Konstantin Zhikhor, 2018-08-20 14:08:15

How to make multi-nested table entry in Yii in create?

Help please with a problem. Can not decide. More precisely decided on half.
I need to make a block with steps and sub-steps.
Here is how it all looks in view.
5b7a9ef40956d524288441.png
It is clear that there can be many stages with points, and there can also be an unlimited number of sub-stages in them.
The problem is that in the creation, only stages are created. I figured it out with them, but the stages do not want to be loaded into the model.
here is the code of view stages and sub-stages

<?= \unclead\multipleinput\TabularInput::widget([
    'models'=>$model,
    'addButtonPosition'=>\unclead\multipleinput\MultipleInput::POS_FOOTER,
    'rendererClass' => \unclead\multipleinput\renderers\ListRenderer::className(),
    'addButtonOptions'=>['class'=>'ag_2_1', 'label'=>'Добавить этап'],
    'layoutConfig'=>['offsetClass'=>'','labelClass'=>'','wrapperClass'=>'', 'errorClass'=>''],
    'columns' => [
        [
            'name'=>'title',
            'options'=>[
                'class'=>'input_goal'
            ]
        ],
        [
            'name'=>'description',
            'type'=>'textarea',
            'options'=>[
                'class'=>'input_goal'
            ]
        ],
        [
            'name'=>'date_finish_stage',
            'type'=>\kartik\date\DatePicker::className(),
            'options'=>[

            ]
        ],
        [
            'name'=>'Substages',
            'type'=>\unclead\multipleinput\MultipleInput::className(),
            'options'=>[
                'columns'=>[
                    [
                        'name'=>'text',
                        'type'=>\unclead\multipleinput\MultipleInputColumn::TYPE_TEXT_INPUT
                    ]
                ]
            ]
        ],
    ]
])
?>

here is the actionCreate itself. Don't look at stupid code. I just need to suggest what I need to do so that the sub-stages are entered into the model.
public function actionCreate()
    {
        $model = new Goals;
        $Stages = [new Stage()];
    $criterias = CriteriesGoals::find()->all();
        $Substages = ;
        if ($model->load(Yii::$app->request->post())) {
      $model->id_user = Yii::$app->user->id;
      $model->status = self::ACTIVEGOAL;
      $model->doc = UploadedFile::getInstance($model, 'doc');
      
      if ($model->doc) {
        if($model->upload())
        {
          $model->doc = $model->doc->name;
        }
      }

            $Substages[] = new Substage;
            Model::loadMultiple($Substages, Yii::$app->request->post('Stage'));
            print_r(Yii::$app->request->post('Stage'));
      die();
      if($model->save())
      {
                $items = Yii::$app->request->post('Stage',[]);
                for($i = 0; $i<count($items); $i++){
                    $Stages[$i] = new Stage();
                }
        if(Model::loadMultiple($Stages, Yii::$app->request->post()) && Model::validateMultiple($Stages))
        {

          foreach ($Stages as $key=>$Stage) {
            $Stage->id_user = Yii::$app->user->id;
            $Stage->goal_id = $model->id;

            $Stage->save(false);
                        /*$Substages[$key] = new Substage();
                        Model::loadMultiple($Substages, Yii::$app->request->post('Stage')['Stage'][$key],'Substages');
                        foreach($Substages as $key2=>$substage){
                            //$substage->text = Yii::$app->user->id;
                            $substage->id_user = Yii::$app->user->id;
                            $substage->id_stage = $Stage->id;

                            //$substage->save(false);
                        }
                        print_r($Substages);*/


                        /*if(Model::loadMultiple($Substages, Yii::$app->request->post('Stage')[$key]['Substage']) && Model::validateMultiple($Substages))
                        {
                            foreach($Substages as $substage){
                                $substage->id_user = Yii::$app->user->id;
                                $substage->id_stage = $Stage->id;
                                $substage->save(false);
                            }
                        }*/
          }
                    //die();
        }

        
        return $this->redirect(['view', 'id' => $model->id]);
      }
        }

        return $this->render('create', [
            'model' => $model,
            'criterias' => $criterias,
            'stages' => $Stages,
            'Substages' => $Substages,
        ]);
    }

And for your convenience, if the printed POST helps you
Array
(
    [_csrf] =&gt; J8d1xixQoLbb9BY6JzaHID5kNymEfY6mupCWXTDz4IlN8kCiZTPMx_a5IlRzTvRuUQ5AU80l3PXgx88aAaKs8A==
    [Goals] =&gt; Array
        (
            [goal] =&gt; купить дом
            [date_finish_goal] =&gt; 2018-09-06
            [criterion_fifnish_goal] =&gt; купленный дом
            [need_goal] =&gt; жить в своем дома
            [category_goal] =&gt; 
            [priority_goal] =&gt; 1
            [is_public] =&gt; 0
            [newPicture] =&gt; 
        )

    [date_finish_goal-goals-date_finish_goal-disp] =&gt; 6 Сентябрь 2018
    [Stage] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [title] =&gt; wwwww
                    [description] =&gt; frgregrg
                    [date_finish_stage] =&gt; 20.08.2018
                )

            [Substages] =&gt; Array
                (
                    [text] =&gt; Array
                        (
                            [0] =&gt; gregregfrfg
                            [1] =&gt; gregregrgr
                        )

                )

        )

)

Please tell me what should I do so that the stages and related sub-stages are entered into the database.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-08-20
@slo_nik

Good afternoon.
And you are not confused by what is in the line

print_r(Yii::$app->request->post('Stage'));
 die();

Are you interrupting script execution?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question