1

I have "components" that I make relation between them , and if I want to "update" the information about some component related to other,I must delete the related "component" and the relation enter image description here

. In actionUpdate I have :

 public function actionUpdate($id) {
    $model = Component::find()->where(['id' => $id])->one();
    $tractorModels = ArrayHelper::map(Tractormodel::find()->all(), 'id', 'model');
    $components = Component::find()->all();
    $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"]]);
       }
        return $this->redirect(['index', 'id' => $model->id]);
    } else {
        return $this->render('update', [
                    'model' => $model, 'tractorModels' => $tractorModels,
                    'components' => $components, 'depModels' => $depModels,
        ]);
    }
}

and in the view I use jQuery to get the deleted items in array with id (deletedIDs) , and to delete the relation and related "items" to the components

 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--;
    });

Everything is ok on "theory" but the components dont delete in the DB , I look if

 if ($deletedIDs && is_array($deletedIDs)) {
        Dependency::deleteAll(['IN', 'id', $deletedIDs]);
   }

work , and I var_dump($deletedIDs) and got the array with the items I want to delete,but the components that are related to the "item" when I delete them don`t disappear. Can someone tell me what I miss ?

8
  • I tried myself and deleteAll method works fine to me (is array and contains multiple IDs. So it probably works in your case, too). What you can try is to use try and get exceptions (to see what's wrong). Commented Aug 15, 2016 at 8:02
  • I try it ,but it dont delete them in the Database , var_dump work , and it seems to catch them and going to delete them , but actually dont delete them , and thats a bit acquard ( here is my var_dump with all elements I want to delete , so I think I miss something but cant figure out what) . And yes , the array cointains every ID from each "component" I want to delete . array(3) { [0]=> string(2) "15" [1]=> string(2) "13" [2]=> string(2) "16" } Commented Aug 15, 2016 at 8:11
  • Ok, then try to delete row directly from database and see if it lets. Commented Aug 15, 2016 at 8:32
  • 1 row deleted. (Query took 0.0550 seconds.) , directly from phpmyadmin and on refresh they remain only 2 "components" related to the "main component" Commented Aug 15, 2016 at 8:41
  • Ok, that was not quite what I have expected. I'll take a look later, right now I'm short on time. Commented Aug 15, 2016 at 9:04

1 Answer 1

1

Solved it :

 Yii::$app->db->creat‌​eCommand()->delete('d‌​ependency', ['dependent_id' => $deletedIDs])->execu‌​te(); 

instead of

Dependency::deleteAl‌​l(['id' => Yii::$app->request->‌​post()["deletedIds"]]‌​); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.