wbraganca / yii2-dynamicform

It is widget to yii2 framework to clone form elements in a nested manner, maintaining accessibility.
Other
436 stars 438 forks source link

Nested dynamic forms #289

Closed IntegrityFoundation closed 5 years ago

IntegrityFoundation commented 5 years ago

Hi, I am using dynamic form widget for nested dynamic form. The scenario is One Pastor has many short term goals and each short term goals has Misssional goals. So I have three tables:

  1. Pastor
  2. Shorttermgoals
  3. Shorttermmissionalgoals actionCreate is implemented well. I have below actionUpdate. When I run it, foll error is displayed.

    Invalid Configuration – yii\base\InvalidConfigException
    The 'model' property must be set and must extend from '\yii\base\Model'.
    public function actionUpdate($id)
    {
    
        $model = $this->findModel($id);
        $modelsShortTerm=$model->shorttermgoals;
        $modelsShortTermMissional = [];
        $oldShortTermMissional = [];
    
        if (!empty($modelsShortTerm)) {
            foreach ($modelsShortTerm as $indexShortTerm => $modelShortTerm) {
                $missional = $modelShortTerm->shorttermmissionalgoals;
                $modelsShortTerm[$indexShortTerm] = $missional;
                $oldShortTermMissional = ArrayHelper::merge(ArrayHelper::index($missional, 'ShortTermMissionalGoalId'), $oldShortTermMissional);
            }
        }
    
        if ($model->load(Yii::$app->request->post())) {
    
            $modelsShortTermMissional = [];
    
        $oldIDs = ArrayHelper::map($modelsShortTerm, 'ShortTermGoalsId', 'ShortTermGoalsId');
    
                $modelsShortTerm = Modelshortterm::createMultiple(Shorttermgoals::classname(), $modelsShortTerm);
    
                Modelshortterm::loadMultiple($modelsShortTerm, Yii::$app->request->post());
    
                $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsShortTerm, 'ShortTermGoalsId', 'ShortTermGoalsId')));
    
                $valid = $model->validate();
                $valid = Modelshortterm::validateMultiple($modelsShortTerm) && $valid;
    
                $missionalId = [];
                if (isset($_POST['Shorttermmissionalgoals'][0][0])) {
                foreach ($_POST['Shorttermmissionalgoals'] as $indexShortTerm => $missional) {
                    $missionalId = ArrayHelper::merge($missionalId, array_filter(ArrayHelper::getColumn($missional, 'ShortTermMissionalGoalId')));
                    foreach ($missional as $indexMissional => $missional1) {
                        $data['Shorttermmissionalgoals'] = $missional1;
                        $modelShortTermMissional = (isset($missional1['ShortTermMissionalGoalId']) && isset($oldShortTermMissional[$missional1['ShortTermMissionalGoalId']])) ? $oldShortTermMissional[$missional1['ShortTermMissionalGoalId']] : new Shorttermmissionalgoals;
                        $modelShortTermMissional->load($data);
                        $modelsShortTermMissional[$indexShortTerm][$indexMissional] = $modelShortTermMissional;
                        $valid = $modelShortTermMissional->validate();
                    }
                }
            }
    
            $oldMissionalId = ArrayHelper::getColumn($oldShortTermMissional, 'ShortTermMissionalGoalId');
            $deletedMissionalId = array_diff($oldMissionalId, $missionalId);
    
                if ($valid) {
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                    if ($flag = $model->save(false)) {
                        if (! empty($deletedMissionalId)) {
                            Shorttermmissionalgoals::deleteAll(['ShortTermMissionalGoalId' => $deletedMissionalId]);
                        }
                        if (!empty($deletedIDs)) {
                            Modelshortterm::deleteAll(['ShortTermGoalsId' => $deletedIDs]);
                        }
                        foreach ($modelsShortTerm as $indexShortTerm => $modelShortTerm) {
    
                            if ($flag === false) {
                                break;
                            }
    
                            $modelShortTerm->PastorId = $model->PastorId;
    
                            if (!($flag = $modelShortTerm->save(false))) {
                                break;
                            }
    
                            if (isset($modelsShortTermMissional[$indexShortTerm]) && is_array($modelsShortTermMissional[$indexShortTerm])) {
                                foreach ($modelsShortTermMissional[$indexShortTerm] as $indexMissional => $modelShortTermMissional) {
                                    $modelShortTermMissional->ShortTermGoalsId = $modelShortTerm->ShortTermGoalsId;
                                    if (!($flag = $modelShortTermMissional->save(false))) {
                                        break;
                                    }
                                }
                            }
                        }
    
                    }
                    if ($flag) {
                        $transaction->commit();
                        return $this->redirect(['view', 'id' => $model->PastorId]);
                    }
                } catch (Exception $e) {
                    $transaction->rollBack();
                }
            }
    
            //return $this->redirect(['view', 'id' => $model->GroupId]);
    
        }else {
            return $this->render('update', [
                'model' => $model,
                'modelsShortTerm' => (empty($modelsShortTerm)) ? [new Shorttermgoals] : $modelsShortTerm,
                                'modelsShortTermMissional' => (empty($modelsShortTermMissional)) ? [[new Shorttermmissionalgoals]] : $modelsShortTermMissional,
            ]);
        }
    }

I am using Modelshortterm,

<?php

namespace app\models;

use Yii;
use yii\helpers\ArrayHelper;

class Modelshortterm extends \yii\base\Model
{
    /**
     * Creates and populates a set of models.
     *
     * @param string $modelClass
     * @param array $multipleModels
     * @return array
     */
    public static function createMultiple($modelClass, $multipleModels = [])
    {
        $model    = new $modelClass;
        $formName = $model->formName();
        $post     = Yii::$app->request->post($formName);
        $models   = [];

        if (! empty($multipleModels)) {
            $keys = array_keys(ArrayHelper::map($multipleModels, 'ShortTermGoalsId', 'ShortTermGoalsId'));
            $multipleModels = array_combine($keys, $multipleModels);
        }

        if ($post && is_array($post)) {
            foreach ($post as $i => $item) {
                if (isset($item['ShortTermGoalsId']) && !empty($item['ShortTermGoalsId']) && isset($multipleModels[$item['ShortTermGoalsId']])) {
                    $models[] = $multipleModels[$item['ShortTermGoalsId']];
                } else {
                    $models[] = new $modelClass;
                }
            }
        }

        unset($model, $formName, $post);

        return $models;
    }
}

Below is the image image 1