Deleting related models in yii2

When I open update "page" I have dynamically rows that are saved in the DB . But how I can manage to delete the rows in actionUpdate to delete them in the DB.The deleted "items" are in array deletedIds


public function actionUpdate($id) {


    $model = Component::find()->where(['id' => $id])->one();


    $depModels = Dependency::find()->where(['component_id' => $id])->all();


    $tractorModels = ArrayHelper::map(Tractormodel::find()->all(), 'id', 'model');


    $components = Component::find()->all();




    if ($model->load(Yii::$app->request->post()) && $model->save()) {


        $dependendComponents = Yii::$app->request->bodyParams['ids'];

        foreach ($dependendComponents as $dComp) {


            $dependencyModel = new Dependency();

            $dependencyModel->setAttributes([

                'count' => $dComp['quantity'],

                'component_id' => $model->id,

                'dependent_id' => $dComp['id']

            ]);

            $dependencyModel->save();

        }


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

    } else {

        return $this->render('update', [

                    'model' => $model, 'tractorModels' => $tractorModels,

                    'components' => $components, 'depModels' => $depModels,

        ]);

    }

}

and here is my remove row jQUery in "_form"


wrapper.on("click", ".remove_field", function (e) {


        var wantedDiv = $(this).parent('div').children().first();

        var selectTag = $(wantedDiv).find('select');


        var clickedId = $(selectTag).find('[selected=""]').attr('value');


        var deletedIdsArray = $('#deletedIDs');


        console.log($('#deletedIDs'));


        if (clickedId) {

            $('#deletedIds').append('<input type="hidden" name="deletedIds[]" value="' + clickedId + '">');

        }


        e.preventDefault();

        $(this).parent('div').remove();

        x--;

    });

Please use "code" tags and left-align your text.

What does your $_POST array look like? Since you are appending hidden fields when you submit the form you should have access to the array of ids.

var deletedIdsArray = $(’#deletedIDs’);

this is my array with the deleted fields , I put the deleted items in an array , and then I want to delete the relation ( also the items) in the database. I try with code:


if ($deletedIDs && is_array($deletedIDs)) {

               Dependency::deleteAll(['id' => Yii::$app->request->post()["deletedIds"]]);

            }

,and when I var_dump it it gives me the array with the deleted items , but dont delete them in the database :


array(3) {

  [0]=>

  string(2) "15"

  [1]=>

  string(2) "13"

  [2]=>

  string(2) "16"

}

This is my code in the action in controller


$depModels = Dependency::find()->where(['component_id' => $id])->all();

        $deletedIDs = Yii::$app->request->post("deletedIds");

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            $dependendComponents = Yii::$app->request->bodyParams['ids'];

            

            foreach ($dependendComponents as $dComp) {

                $dependencyModel = new Dependency();

                $dependencyModel->setAttributes([

                    'count' => $dComp['quantity'],

                    'component_id' => $model->id,

                    'dependent_id' => $dComp['id']

                ]);

           $dependencyModel->save();

                

            }

             if ($deletedIDs && is_array($deletedIDs)) {

               Dependency::deleteAll(['id' => Yii::$app->request->post()["deletedIds"]]);

            }