diff options
author | Thomas Citharel <tcit@tcit.fr> | 2016-09-25 16:11:06 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-25 16:11:06 +0200 |
commit | 7e98ad962680fac17b3b90ae34b9c6e5afe7636f (patch) | |
tree | 451f2c7ec7011f1f0ee8911fb1921b02f158e1dc /src/Wallabag/CoreBundle/Helper | |
parent | 9d7dd6b0d2480d3efff5b0ab1461f2ef99bfd57a (diff) | |
parent | 289875836a09944f5993d33753042abfef13809e (diff) | |
download | wallabag-7e98ad962680fac17b3b90ae34b9c6e5afe7636f.tar.gz wallabag-7e98ad962680fac17b3b90ae34b9c6e5afe7636f.tar.zst wallabag-7e98ad962680fac17b3b90ae34b9c6e5afe7636f.zip |
Merge pull request #2308 from wallabag/tags-duplicate
Fix duplicate tags on import
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/ContentProxy.php | 26 |
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 |