aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/ContentProxy.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/ContentProxy.php')
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index 5dd684f2..8019df42 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -95,14 +95,24 @@ class ContentProxy
95 * Assign some tags to an entry. 95 * Assign some tags to an entry.
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 @see http://stackoverflow.com/a/7879164/569101
99 */ 101 */
100 public function assignTagsToEntry(Entry $entry, $tags) 102 public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = [])
101 { 103 {
102 if (!is_array($tags)) { 104 if (!is_array($tags)) {
103 $tags = explode(',', $tags); 105 $tags = explode(',', $tags);
104 } 106 }
105 107
108 // keeps only Tag entity from the "not yet flushed entities"
109 $tagsNotYetFlushed = [];
110 foreach ($entitiesReady as $entity) {
111 if ($entity instanceof Tag) {
112 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
113 }
114 }
115
106 foreach ($tags as $label) { 116 foreach ($tags as $label) {
107 $label = trim($label); 117 $label = trim($label);
108 118
@@ -111,11 +121,15 @@ class ContentProxy
111 continue; 121 continue;
112 } 122 }
113 123
114 $tagEntity = $this->tagRepository->findOneByLabel($label); 124 if (isset($tagsNotYetFlushed[$label])) {
125 $tagEntity = $tagsNotYetFlushed[$label];
126 } else {
127 $tagEntity = $this->tagRepository->findOneByLabel($label);
115 128
116 if (is_null($tagEntity)) { 129 if (is_null($tagEntity)) {
117 $tagEntity = new Tag(); 130 $tagEntity = new Tag();
118 $tagEntity->setLabel($label); 131 $tagEntity->setLabel($label);
132 }
119 } 133 }
120 134
121 // only add the tag on the entry if the relation doesn't exist 135 // only add the tag on the entry if the relation doesn't exist