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

quote characters used directly in a query #32

Merged
merged 1 commit into from
Jan 20, 2015

Conversation

xabbuh
Copy link
Collaborator

@xabbuh xabbuh commented Jan 17, 2015

This fixes #26.

@@ -385,6 +385,7 @@ protected function findBy($entityClass, $searchQuery, array $searchableFields, $
;

foreach ($searchableFields as $name => $metadata) {
$searchQuery = $this->getDoctrine()->getConnection()->quote($searchQuery);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have two comments about this change:

  1. This line should be moved outside the foreach loop to not repeat it for each field.
  2. I don't think that calling the quote() method is the right thing to do here. I've made some tests:
What the user typed in the search box The result of quote() The expected result
foo' 'foo\'' foo\'
%foo% '%foo%' \%foo\%

Keep in mind that the query is wrapped like this to do the full-text search: '%'.$searchQuery.'%' This means that it cannot be quoted, we just have to escape the special chars in the context of the LIKE query.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, you're right.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does adding $searchQuery = trim($searchQuery, "'"); seems a bad practice ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's not a good idea because we then relied on implementation specific behaviour.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to issue #26 the best solution might seem this one:

$wildcards = $this->getDoctrine()->getConnection()->getDatabasePlatform()->getWildcards();
if (!count($wildcards)) {
    $wildcards = array('%','_');
}
$wildcards = implode('', $wildcards);
$searchQuery = addcslashes($searchQuery,$wildcards);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the code.

@javiereguiluz
Copy link
Collaborator

Thank you Christian! I appreciate the time you took to solve this PR and do all the asked code changes.

@javiereguiluz javiereguiluz merged commit c6495f8 into EasyCorp:master Jan 20, 2015
javiereguiluz added a commit that referenced this pull request Jan 20, 2015
This PR was merged into the master branch.

Discussion
----------

quote characters used directly in a query

This fixes #26.

Commits
-------

c6495f8 quote characters used directly in a query
@xabbuh xabbuh deleted the issue-26 branch January 20, 2015 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Escape special chars in the LIKE queries of search action
3 participants