Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept less strict search like %foo%bar% #2473

Merged
merged 1 commit into from
Mar 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Repository/ContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bolt\Repository;

use Bolt\Common\Str;
use Bolt\Configuration\Content\ContentType;
use Bolt\Doctrine\JsonHelper;
use Bolt\Entity\Content;
Expand Down Expand Up @@ -98,6 +99,10 @@ public function searchNaive(string $searchTerm, int $page, int $amountPerPage, C
$connection = $qb->getEntityManager()->getConnection();
[$where] = JsonHelper::wrapJsonFunction('t.value', $searchTerm, $connection);

// Rather than searching for '%foo bar%', search '%foo%bar%' which doesn't require
// an exact match, but requires 'foo' to appear before 'bar'.
$searchTerm = str_replace(' ', '%', Str::cleanWhitespace($searchTerm));

// The search term must match the format of the content in the database
// Therefore, it is JSON encoded and escaped with backslashes
$encodedSearchTerm = addslashes(trim(json_encode($searchTerm), '"'));
Expand Down