aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/ContentProxy.php
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-09-25 11:26:15 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-09-25 12:03:49 +0200
commit401135852c6b25c8d5ab97beaefb02d1bd023ec9 (patch)
tree5922f4bd40af0ceb61db2c55755bc006f18410fb /src/Wallabag/CoreBundle/Helper/ContentProxy.php
parentfaa86e06ba3032fdb98f3c0f79c72e8581d3c96f (diff)
downloadwallabag-401135852c6b25c8d5ab97beaefb02d1bd023ec9.tar.gz
wallabag-401135852c6b25c8d5ab97beaefb02d1bd023ec9.tar.zst
wallabag-401135852c6b25c8d5ab97beaefb02d1bd023ec9.zip
Use scheduled entity insertions to avoid tag duplicate
Using `getScheduledEntityInsertions()` we can retrieve not yet flushed but already persisted entities and then avoid tags duplication on import.
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/ContentProxy.php')
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php25
1 files changed, 20 insertions, 5 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index 5dd684f2..a65a21e8 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -96,13 +96,24 @@ class ContentProxy
96 * 96 *
97 * @param Entry $entry 97 * @param Entry $entry
98 * @param array|string $tags An array of tag or a string coma separated of tag 98 * @param array|string $tags An array of tag or a string coma separated of tag
99 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
100 * It is mostly to fix duplicate tag on import
101 * @see http://stackoverflow.com/a/7879164/569101
99 */ 102 */
100 public function assignTagsToEntry(Entry $entry, $tags) 103 public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = [])
101 { 104 {
102 if (!is_array($tags)) { 105 if (!is_array($tags)) {
103 $tags = explode(',', $tags); 106 $tags = explode(',', $tags);
104 } 107 }
105 108
109 // keeps only Tag entity from the "not yet flushed entities"
110 $tagsNotYetFlushed = [];
111 foreach ($entitiesReady as $entity) {
112 if ($entity instanceof Tag) {
113 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
114 }
115 }
116
106 foreach ($tags as $label) { 117 foreach ($tags as $label) {
107 $label = trim($label); 118 $label = trim($label);
108 119
@@ -111,11 +122,15 @@ class ContentProxy
111 continue; 122 continue;
112 } 123 }
113 124
114 $tagEntity = $this->tagRepository->findOneByLabel($label); 125 if (isset($tagsNotYetFlushed[$label])) {
126 $tagEntity = $tagsNotYetFlushed[$label];
127 } else {
128 $tagEntity = $this->tagRepository->findOneByLabel($label);
115 129
116 if (is_null($tagEntity)) { 130 if (is_null($tagEntity)) {
117 $tagEntity = new Tag(); 131 $tagEntity = new Tag();
118 $tagEntity->setLabel($label); 132 $tagEntity->setLabel($label);
133 }
119 } 134 }
120 135
121 // only add the tag on the entry if the relation doesn't exist 136 // only add the tag on the entry if the relation doesn't exist