2 Replies - 240 Views - Last Post: 04 January 2018 - 01:07 PM Rate Topic: -----

#1 comosejama   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 25-September 16

Compare two arrays of data and save this data to database

Posted 04 January 2018 - 12:16 PM

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;
} else {
var_dump("add new label => " . $labelFromFile);

                    var_dump("Remove label => ");

        //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 :)/>/>

Is This A Good Question/Topic? 0
  • +

Replies To: Compare two arrays of data and save this data to database

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,269
  • Joined: 03-December 12

Re: Compare two arrays of data and save this data to database

Posted 04 January 2018 - 01:01 PM

If I am understanding, the database should be updated only if the list in the file is different?
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5239
  • View blog
  • Posts: 14,035
  • Joined: 18-April 07

Re: Compare two arrays of data and save this data to database

Posted 04 January 2018 - 01:07 PM

Ok I will take a crack at this and hopefully I am on the right track to what you are looking to do. Let's start with the database schema first to understand a few concepts that may help.

You said you have a many to many relationship between an article's labels and labels have many articles. I assume you have this in the database setup through a joining table. The joining table would have an article id and a label id and together they form a unique record. Thus you can have many rows with the same article id but different label ids and vice versa, same label id with many different article ids.

So what you will need to do is get a list of all labels in the database and create a list (an array will do). As you run through the file and each of the article's labels, you can search this list and remove the ones found on the article. Think of it as a process of elimination. After you run through all articles in the file, the labels left in the list are the ones that need to be removed from the database.

Coming back around to our database schema now, you should be able to simply delete labels from the labels table. I assume you also have a cascading delete, so if you delete from the labels it would automatically delete the corresponding records in the joining tables (not touching articles in the articles table).

I don't think you can delete any labels as you go along because you don't know if that label will appear on another article further down in the file. You have to sum up what labels you do see in the entire file and then run the code on the database to delete any labels that don't appear.

Does that sound about right? :)

This post has been edited by Martyr2: 04 January 2018 - 01:08 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1