diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2017-08-03 12:46:20 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2017-09-06 22:49:15 +0200 |
commit | 7c04b7396a296e31bb11beadc19550396ee728a8 (patch) | |
tree | 907da35635e18957fc981cb6d53b98dd6040290c /src | |
parent | 78b36d4dbeedd60c5aa25dbd30a2a2d41a658f94 (diff) | |
download | wallabag-7c04b7396a296e31bb11beadc19550396ee728a8.tar.gz wallabag-7c04b7396a296e31bb11beadc19550396ee728a8.tar.zst wallabag-7c04b7396a296e31bb11beadc19550396ee728a8.zip |
Multiple tag search was broken from API
First, the setParameter() were done on the same parameter which in fact
just duplicated the condition in the SQL query (like `where t.label =
'test' and t.label = 'test'`.
Changed the parameter doesn't help because the query was then wrong.
Changing the way to match associated tags for an entry and it worked.
Diffstat (limited to 'src')
-rw-r--r-- | src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php | 9 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Repository/EntryRepository.php | 45 |
2 files changed, 48 insertions, 6 deletions
diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php index 6de561e0..0ecfd18b 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadTagData.php | |||
@@ -19,7 +19,7 @@ class LoadTagData extends AbstractFixture implements OrderedFixtureInterface | |||
19 | 19 | ||
20 | $manager->persist($tag1); | 20 | $manager->persist($tag1); |
21 | 21 | ||
22 | $this->addReference('foo-tag', $tag1); | 22 | $this->addReference('foo-bar-tag', $tag1); |
23 | 23 | ||
24 | $tag2 = new Tag(); | 24 | $tag2 = new Tag(); |
25 | $tag2->setLabel('bar'); | 25 | $tag2->setLabel('bar'); |
@@ -35,6 +35,13 @@ class LoadTagData extends AbstractFixture implements OrderedFixtureInterface | |||
35 | 35 | ||
36 | $this->addReference('baz-tag', $tag3); | 36 | $this->addReference('baz-tag', $tag3); |
37 | 37 | ||
38 | $tag4 = new Tag(); | ||
39 | $tag4->setLabel('foo'); | ||
40 | |||
41 | $manager->persist($tag4); | ||
42 | |||
43 | $this->addReference('foo-tag', $tag4); | ||
44 | |||
38 | $manager->flush(); | 45 | $manager->flush(); |
39 | } | 46 | } |
40 | 47 | ||
diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index ecc159fc..44f95dcb 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php | |||
@@ -133,7 +133,7 @@ class EntryRepository extends EntityRepository | |||
133 | { | 133 | { |
134 | $qb = $this->createQueryBuilder('e') | 134 | $qb = $this->createQueryBuilder('e') |
135 | ->leftJoin('e.tags', 't') | 135 | ->leftJoin('e.tags', 't') |
136 | ->where('e.user =:userId')->setParameter('userId', $userId); | 136 | ->where('e.user = :userId')->setParameter('userId', $userId); |
137 | 137 | ||
138 | if (null !== $isArchived) { | 138 | if (null !== $isArchived) { |
139 | $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived); | 139 | $qb->andWhere('e.isArchived = :isArchived')->setParameter('isArchived', (bool) $isArchived); |
@@ -152,8 +152,23 @@ class EntryRepository extends EntityRepository | |||
152 | } | 152 | } |
153 | 153 | ||
154 | if ('' !== $tags) { | 154 | if ('' !== $tags) { |
155 | foreach (explode(',', $tags) as $tag) { | 155 | foreach (explode(',', $tags) as $i => $tag) { |
156 | $qb->andWhere('t.label = :label')->setParameter('label', $tag); | 156 | $entryAlias = 'e' . $i; |
157 | $tagAlias = 't' . $i; | ||
158 | |||
159 | // Complexe queries to ensure multiple tags is associated to an entry | ||
160 | // https://stackoverflow.com/a/6638146/569101 | ||
161 | $qb->andWhere($qb->expr()->in( | ||
162 | 'e.id', | ||
163 | $this->createQueryBuilder($entryAlias) | ||
164 | ->select($entryAlias . '.id') | ||
165 | ->leftJoin($entryAlias . '.tags', $tagAlias) | ||
166 | ->where($tagAlias . '.label = :label' . $i) | ||
167 | ->getDQL() | ||
168 | )); | ||
169 | |||
170 | // bound parameter to the main query builder | ||
171 | $qb->setParameter('label' . $i, $tag); | ||
157 | } | 172 | } |
158 | } | 173 | } |
159 | 174 | ||
@@ -181,7 +196,7 @@ class EntryRepository extends EntityRepository | |||
181 | ->innerJoin('e.tags', 't') | 196 | ->innerJoin('e.tags', 't') |
182 | ->innerJoin('e.user', 'u') | 197 | ->innerJoin('e.user', 'u') |
183 | ->addSelect('t', 'u') | 198 | ->addSelect('t', 'u') |
184 | ->where('e.user=:userId')->setParameter('userId', $userId) | 199 | ->where('e.user = :userId')->setParameter('userId', $userId) |
185 | ; | 200 | ; |
186 | 201 | ||
187 | return $qb->getQuery()->getResult(); | 202 | return $qb->getQuery()->getResult(); |
@@ -323,7 +338,27 @@ class EntryRepository extends EntityRepository | |||
323 | { | 338 | { |
324 | $qb = $this->createQueryBuilder('e') | 339 | $qb = $this->createQueryBuilder('e') |
325 | ->select('count(e)') | 340 | ->select('count(e)') |
326 | ->where('e.user=:userId')->setParameter('userId', $userId) | 341 | ->where('e.user = :userId')->setParameter('userId', $userId) |
342 | ; | ||
343 | |||
344 | return (int) $qb->getQuery()->getSingleScalarResult(); | ||
345 | } | ||
346 | |||
347 | /** | ||
348 | * Count all entries for a tag and a user. | ||
349 | * | ||
350 | * @param int $userId | ||
351 | * @param int $tagId | ||
352 | * | ||
353 | * @return int | ||
354 | */ | ||
355 | public function countAllEntriesByUserIdAndTagId($userId, $tagId) | ||
356 | { | ||
357 | $qb = $this->createQueryBuilder('e') | ||
358 | ->select('count(e.id)') | ||
359 | ->leftJoin('e.tags', 't') | ||
360 | ->where('e.user = :userId')->setParameter('userId', $userId) | ||
361 | ->andWhere('t.id = :tagId')->setParameter('tagId', $tagId) | ||
327 | ; | 362 | ; |
328 | 363 | ||
329 | return (int) $qb->getQuery()->getSingleScalarResult(); | 364 | return (int) $qb->getQuery()->getSingleScalarResult(); |