0

For an autocomplete, I'm searching a table of address (full address in one column).

Example: 123 Example building, Somestreet, Town, QW1E 2RT

To improve the search I want to remove the space, so you can search QW1E2RT or QW1E 2RT, however, I obviously don't want that to be displayed to the user.

I think I'm most of the way there:

$query = \Drupal::database()->select('mctable', 'mc');
$query->addField('mc', 'a_address');
$query->where('REPLACE(mc.a_address, \' \', \'\') LIKE \':value\'', [':value' => '%' . $string . '%']);
$query->distinct();
$rows = $query->execute();

The problem is that the :value placeholder doesn't appear to be passed through… If I hardcode a value in the "snippet" it works.

I originally tried with:

$query->addExpression('REPLACE(mc.a_address, \' \', \'\')', 'a_address');
$query->condition('a_address','%' . $query->escapeLike($string) . '%', 'LIKE');

But couldn't figure out how to use the expression in the condition.

Many Thanks for reading and helping.

1 Answer 1

0

The problem with the code above is the apostrophes around the placeholder.

LIKE \':value\'', should be LIKE :value',.

I would still like to know how to do this with addExpression though :-/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.