I have problem with update my database data with data from file. I have file with some data and I have to save this data in database and I have to delete the data which is not in the file. Problem is that every row with data have labels which I need update in the same way.

So I have array of my data from file and array of data from database and I am trying to compare this arrays and update my database based on file data. I can't use truncate or something like this becouse I need to have continuity of this data. I am writing in Symphony and I have Article with collections of of Labels and Label with collections on Articles. Many to many.

  1. Here we have all article from database. Every article have collection of Labels
  2. For every row of file
  3. I compare every row of database
  4. If I will find te article in database
  5. In this case I need to check if labels for this article have changed in file
  6. So I create array of labels for this article from collection to compare with labels from file
  7. And I am checking in the same way for every label from file I compare label from database
  8. I think here is my main problem. If i will find this label in databaseI should not do anything. I have this Label in database and I can continue.
  9. I can add label to database if I havent it in database yet.
  10. I need remove Label from database if I havent it in File. But I havent idea how can I do this. I have tried to do some arrays of this articles. But it failed.

/*1*/  $articlesFromDatabase = $this->em->getRepository('AppBundle:Article')->findBy(['project' => $this->project]);

 /*2*/    foreach ($this->articlesFromFile as $articleFromFile) {
            $articleFoundInDatabase = false;

 /*3*/       foreach ($articlesFromDatabase as $articleFromDatabase) {

  /*4*/          if ($articleFromDatabase->getName() == $articleFromFile['article']) {
                    $articleFoundInDatabase = true;

 /*5, 6*/             $labelsFromDatabase = array_map(create_function('$label', 'return $label->getName();'), $articleFromDatabase->getLabels()->toArray());

 /*7*/                foreach ($articleFromFile['labels'] as $articleFromFile) {
                        $labelIsInDatabase = false;

 /*7*/                    foreach ($labelsFromDatabase as $labelFromDatabase) {

 /*8*/                        if ($labelFromDatabase == $labelFromFile) {
                                $labelIsInDatabase = true;
                                continue;
                            } else {
         /*9*/
                                var_dump("add new label => " . $labelFromFile);
                                exit();
                            }

                        }
             //10??
                        var_dump("Remove label => ");
                        exit();
                    }
                }
            }

            //Need remove article too if I havent it in file
        }

I havent idea how can I remove From this array or this database labels and articles which is not in the file. I cant truncate my table becouse I need to have history of articles. Maybe there is a better way to maintaining the database status from a file. To Remove From database when data in file will change and add new articles and labels when it will change in file. Maybe some one help me to create easy way to accomplish this. Thanks :)