yii2 dynamic form updating only one table
<p>I'm trying to update data with the help of yii2/dynamic form.
Table 1- "sellsg" - columns (ssg_id,ssg_customer,ssg_invoiceno,ssg_date,ssg_amount)
Table2 - "sellitemsg" - columns (ssgi_id,ssgi_invoiceno,ssgi_sgname,ssgi_price)</p>

<p>Relation - sellsg.ssg_invoiceno = sellitemsg.ssgi_invoiceno</p>

<p>The Create part is working fine. I'm struggling with the update part.
<strong>Problem 1</strong>. 
When the form was creating new record - There is a javascript code in the form to increase the invoice no by 1 everytime. When I'm loading the form for update, it also increases in by 1 overriding the original invoiceno. I want to limit this function only when I create but not in update.</p>

<p><strong>Problem 2</strong>
When I am updating the form, it only updating the sellsg table but not sellitemsg table.</p>

<p>_Form</p>

<pre><code>&lt;?php

use yii\helpers\Html;
use kartik\form\ActiveForm;
use wbraganca\dynamicform\DynamicFormWidget;
use dosamigos\datepicker\DatePicker;
use yii\helpers\ArrayHelper;
use kartik\select2\Select2;
use frontend\modules\sellsg\models\Customer;
use frontend\modules\sellsg\models\Sunglass;

/* @var $this yii\web\View */
/* @var $model frontend\modules\sellsg\models\Sellsg */
/* @var $form yii\widgets\ActiveForm */
?&gt;

&lt;div class="sellsg-form"&gt;

    &lt;?php $form = ActiveForm::begin([
        'id' =&gt; 'dynamic-form',
        'type' =&gt; ActiveForm::TYPE_HORIZONTAL,
        'formConfig' =&gt; ['labelSpan' =&gt; 3, 'deviceSize' =&gt; ActiveForm::SIZE_SMALL]
        ]); 
    ?&gt;
    &lt;div class="row"&gt;
        &lt;div class="col-xs-12 col-sm-12 col-lg-12"&gt;
            &lt;div class="form-group"&gt;
                &lt;div class="col-xs-6 col-sm-6 col-lg-6"&gt;
                    &lt;?= $form-&gt;field($model, 'ssg_customer')-&gt;label(false)-&gt;widget(Select2::classname(), [
                        'data' =&gt; ArrayHelper::map(Customer::find()-&gt;all(),'c_name','customerDetails'),
                        'language' =&gt; 'en',
                        'options' =&gt; ['placeholder' =&gt; 'Select Customer Details', 'id' =&gt; 'custid'],
                        'pluginOptions' =&gt; [
                            'allowClear' =&gt; true
                        ],
                        ]); 
                    ?&gt;
                &lt;/div&gt;
                &lt;div class="col-xs-3 col-sm-3 col-lg-3"&gt;
                    &lt;?= $form-&gt;field($model, 'ssg_date')-&gt;widget(
                    DatePicker::className(), [
                    // inline too, not bad
                     'inline' =&gt; false,

                     // modify template for custom rendering
                    //'template' =&gt; '&lt;div class="well well-sm" style="background-color: #fff; width:250px"&gt;{input}&lt;/div&gt;',
                    'clientOptions' =&gt; [
                        'autoclose' =&gt; true,
                        'todayHighlight' =&gt; true,

                        'format' =&gt; 'yyyy-mm-dd'
                    ]
                    ]);?&gt;
                &lt;/div&gt;
                &lt;div class="col-xs-3 col-sm-3 col-lg-3"&gt;

                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;?= $form-&gt;field($model, 'ssg_invoiceno')-&gt;textInput() ?&gt;

    &lt;div class="rows"&gt;
        &lt;div class="panel panel-default"&gt;
        &lt;div class="panel-heading"&gt;&lt;h4&gt;&lt;i class="glyphicon glyphicon-envelope"&gt;&lt;/i&gt; Sunglasses&lt;/h4&gt;&lt;/div&gt;
        &lt;div class="panel-body"&gt;
             &lt;?php DynamicFormWidget::begin([
                'widgetContainer' =&gt; 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
                'widgetBody' =&gt; '.container-items', // required: css class selector
                'widgetItem' =&gt; '.item', // required: css class
                'limit' =&gt; 20, // the maximum times, an element can be cloned (default 999)
                'min' =&gt; 1, // 0 or 1 (default 1)
                'insertButton' =&gt; '.add-item', // css class
                'deleteButton' =&gt; '.remove-item', // css class
                'model' =&gt; $modelsSellitemsg[0],
                'formId' =&gt; 'dynamic-form',
                'formFields' =&gt; [
                    'ssgi_sgname',
                    'ssgi_price',
                ],
            ]); ?&gt;

            &lt;div class="container-items"&gt;&lt;!-- widgetContainer --&gt;
            &lt;?php foreach ($modelsSellitemsg as $i =&gt; $modelSellitemsg): ?&gt;
                &lt;div class="item panel panel-default"&gt;&lt;!-- widgetBody --&gt;
                    &lt;div class="panel-body"&gt;
                        &lt;?php
                            // necessary for update action.
                            if (! $modelSellitemsg-&gt;isNewRecord) {
                                echo Html::activeHiddenInput($modelSellitemsg, "[{$i}]ssgi_id");
                            }
                        ?&gt;
                        &lt;div class="row"&gt;
                            &lt;div class="col-sm-6"&gt;
                                &lt;?= $form-&gt;field($modelSellitemsg, "[{$i}]ssgi_sgname")-&gt;label(false)-&gt;widget(Select2::classname(), [
                                    'data' =&gt; ArrayHelper::map(Sunglass::find()-&gt;all(),'sg_name','sg_name'),
                                    'language' =&gt; 'en',
                                    'options' =&gt; ['placeholder' =&gt; 'Select Sunglass'],
                                    'pluginOptions' =&gt; [
                                        'allowClear' =&gt; true
                                    ],
                                    ]); 
                                ?&gt;
                            &lt;/div&gt;
                            &lt;div class="col-sm-3"&gt;
                                &lt;?= $form-&gt;field($modelSellitemsg, "[{$i}]ssgi_price")-&gt;textInput([
                                'maxlength' =&gt; true,
                                'class' =&gt; 'sumPart',
                                //'onfocus'=&gt;'sum()', 'onBlur'=&gt;'sum()'
                                ]) ?&gt;
                            &lt;/div&gt;
                            &lt;div class="col-sm-3"&gt;
                                &lt;div class="pull-right"&gt;
                                    &lt;button type="button" class="add-item btn btn-success btn-xs"&gt;&lt;i class="glyphicon glyphicon-plus"&gt;&lt;/i&gt;&lt;/button&gt;
                                    &lt;button type="button" class="remove-item btn btn-danger btn-xs"&gt;&lt;i class="glyphicon glyphicon-minus"&gt;&lt;/i&gt;&lt;/button&gt;
                                &lt;/div&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;&lt;!-- .row --&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;?php endforeach; ?&gt;
            &lt;/div&gt;
            &lt;?php DynamicFormWidget::end(); ?&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class="row"&gt;
        &lt;div class="col-xs-12 col-sm-12 col-lg-12"&gt;
            &lt;div class="form-group"&gt;
                &lt;div class="col-xs-6 col-sm-6 col-lg-6"&gt;

                &lt;/div&gt;
                &lt;div class="col-xs-3 col-sm-3 col-lg-3"&gt;
                    &lt;?= $form-&gt;field($model, 'ssg_amount')-&gt;textInput(['class' =&gt; 'sum']) ?&gt;
                &lt;/div&gt;
                &lt;div class="col-xs-3 col-sm-3 col-lg-3"&gt;

                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;/div&gt;
    &lt;div class="col-xs-12 col-sm-12 col-lg-12"&gt;
    &lt;div class="form-group"&gt;
        &lt;?= Html::submitButton($model-&gt;isNewRecord ? 'Create' : 'Update', ['class' =&gt; $model-&gt;isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?&gt;
    &lt;/div&gt;
    &lt;/div&gt;

    &lt;?php ActiveForm::end(); ?&gt;

&lt;/div&gt;

&lt;?php
/* start getting the invoiceno */
$script = &lt;&lt;&lt;EOD
  $(window).load(function(){
  $.get('get-for-invoiceno',{ invoiceid : 1 }, function(data){
      //alert(data);
      var data = $.parseJSON(data);
      $('#sellsg-ssg_invoiceno').attr('value',data.invoiceno);
  }
  );
});
EOD;
$this-&gt;registerJs($script);
/*end getting the invoiceno */
?&gt;

&lt;?php
/* start getting the totalamount */
$script = &lt;&lt;&lt;EOD
    var getSum = function() {

        var items = $(".item");
        var sum = 0;

        items.each(function (index, elem) {
            var priceValue = $(elem).find(".sumPart").val();
            //Check if priceValue is numeric or something like that
            sum = parseInt(sum) + parseInt(priceValue);
        });
        //Assign the sum value to the field
        $(".sum").val(sum);
    };

    //Bind new elements to support the function too
    $(".container-items").on("change", ".sumPart", function() {
        getSum();
    });
EOD;
$this-&gt;registerJs($script);
/*end getting the totalamount */
?&gt;
</code></pre>

<p>SellsgController</p>

<pre><code>public function actionCreate()
    {
        $model = new Sellsg();
        $modelsSellitemsg = [new Sellitemsg];

        if ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;save()) {

            $modelsSellitemsg = Model::createMultiple(Sellitemsg::classname());
            Model::loadMultiple($modelsSellitemsg, Yii::$app-&gt;request-&gt;post());

            // validate all models
            $valid = $model-&gt;validate();
            $valid = Model::validateMultiple($modelsSellitemsg) &amp;&amp; $valid;

            if ($valid) {
                $transaction = \Yii::$app-&gt;db-&gt;beginTransaction();
                try {
                    if ($flag = $model-&gt;save(false)) {
                        foreach ($modelsSellitemsg as $modelSellitemsg) {
                            $modelSellitemsg-&gt;ssgi_invoiceno = $model-&gt;ssg_invoiceno;
                            if (! ($flag = $modelSellitemsg-&gt;save(false))) {
                                $transaction-&gt;rollBack();
                                break;
                            }
                        }
                    }
                    if ($flag) {
                        $transaction-&gt;commit();
                        return $this-&gt;redirect(['view', 'id' =&gt; $model-&gt;ssg_id]);
                    }
                } catch (Exception $e) {
                    $transaction-&gt;rollBack();
                }
            }
        } 
        else {
            return $this-&gt;render('create', [
                'model' =&gt; $model,
                'modelsSellitemsg' =&gt; (empty($modelsSellitemsg)) ? [new Sellitemsg] : $modelsSellitemsg
            ]);
        }
    }

    /**
     * Updates an existing Sellsg model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this-&gt;findModel($id);
        $modelsSellitemsg = $model-&gt;sellitemsg;

        if ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;save()) {

            $oldIDs = ArrayHelper::map($modelsSellitemsg, 'id', 'id');
            //var_dump($oldIDs);
            $modelsSellitemsg = Model::createMultiple(Sellitemsg::classname(), $modelsSellitemsg);
            Model::loadMultiple($modelsSellitemsg, Yii::$app-&gt;request-&gt;post());
            $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsSellitemsg, 'id', 'id')));

            // validate all models
            $valid = $model-&gt;validate();
            $valid = Model::validateMultiple($modelsSellitemsg) &amp;&amp; $valid;

            if ($valid) {
                $transaction = \Yii::$app-&gt;db-&gt;beginTransaction();
                try {
                    if ($flag = $model-&gt;save(false)) {
                        if (! empty($deletedIDs)) {
                            Sellitemsg::deleteAll(['id' =&gt; $deletedIDs]);
                        }
                        foreach ($modelsSellitemsg as $modelSellitemsg) {
                            $modelSellitemsg-&gt;ssgi_invoiceno = $model-&gt;ssg_invoiceno;
                            if (! ($flag = $modelSellitemsg-&gt;save(false))) {
                                $transaction-&gt;rollBack();
                                break;
                            }
                        }
                    }
                    if ($flag) {
                        $transaction-&gt;commit();
                        return $this-&gt;redirect(['view', 'id' =&gt; $model-&gt;ssg_id]);
                    }
                } catch (Exception $e) {
                    $transaction-&gt;rollBack();
                }
            }
        }

        else {
            return $this-&gt;render('update', [
                'model' =&gt; $model,
                'modelsSellitemsg' =&gt; (empty($modelsSellitemsg)) ? [new Sellitemsg] : $modelsSellitemsg
            ]);
        }
    }
</code></pre>

<p>sellsg model</p>

<pre><code>&lt;?php

namespace frontend\modules\sellsg\models;

use Yii;

/**
 * This is the model class for table "sellsg".
 *
 * @property integer $ssg_id
 * @property string $ssg_customer
 * @property integer $ssg_invoiceno
 * @property string $ssg_date
 * @property integer $ssg_amount
 */
class Sellsg extends \yii\db\ActiveRecord
{
    public $modelsSellitemsg;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sellsg';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['ssg_customer', 'ssg_invoiceno', 'ssg_date', 'ssg_amount'], 'required'],
            [['ssg_invoiceno', 'ssg_amount'], 'integer'],
            [['ssg_customer'], 'string', 'max' =&gt; 200],
            [['ssg_invoiceno'], 'unique'],
            [['ssg_date'], 'string', 'max' =&gt; 10], 
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ssg_id' =&gt; 'ID',
            'ssg_customer' =&gt; 'Customer',
            'ssg_invoiceno' =&gt; 'Invoice No',
            'ssg_date' =&gt; 'Date',
            'ssg_amount' =&gt; 'Amount',
        ];
    }
    public function getSellitemsg()
        {
            return $this-&gt;hasMany(Sellitemsg::className(), ['ssgi_invoiceno' =&gt; 'ssg_invoiceno']);
        }
}
</code></pre>

<p>update.php</p>

<pre><code>&lt;?php

use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $model frontend\modules\sellsg\models\Sellsg */

$this-&gt;title = 'Update Sellsg: ' . $model-&gt;ssg_id;
$this-&gt;params['breadcrumbs'][] = ['label' =&gt; 'Sellsgs', 'url' =&gt; ['index']];
$this-&gt;params['breadcrumbs'][] = ['label' =&gt; $model-&gt;ssg_id, 'url' =&gt; ['view', 'id' =&gt; $model-&gt;ssg_id]];
$this-&gt;params['breadcrumbs'][] = 'Update';
?&gt;
&lt;div class="sellsg-update"&gt;

    &lt;h1&gt;&lt;?= Html::encode($this-&gt;title) ?&gt;&lt;/h1&gt;

    &lt;?= $this-&gt;render('_form', [
        'model' =&gt; $model,
        'modelsSellitemsg' =&gt; $modelsSellitemsg,
    ]) ?&gt;

&lt;/div&gt;
</code></pre>

<p>With the current code I'm getting error</p>

<pre><code>Getting unknown property: frontend\modules\sellsg\models\Sellitemsg::id
</code></pre>

<p><a href="https://i.stack.imgur.com/YSYh4.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YSYh4.jpg" alt="enter image description here"></a></p>