gonzo
gonzo

Reputation: 145

CakePHP3: How can I do text searching using full-text indexes

I used the below code to search using full-text indexes

$query = $this->Posts->find()
    ->contain(['Categories' ])
    ->where([
        'MATCH(Posts.title) AGAINST(? IN BOOLEAN MODE)' => $search
]);

But I got the below error

SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters

Please advise

Thanks in advance!

Upvotes: 2

Views: 1619

Answers (1)

Inigo Flores
Inigo Flores

Reputation: 4469

Try rewriting your query as:

$query = $this->Posts->find()
    ->contain(['Categories'])
    ->where([
        "MATCH(Posts.title) AGAINST(:search IN BOOLEAN MODE)" 
    ])
    ->bind(':search', $search);

Make sure you have the latest CakePHP 3.x version installed, as this was added no so long ago.

The following will work, but be warned it makes your code vulnerable to SQL injection attacks:

// for educational purposes only. Don't use in production environments
$query = $this->Posts->find() 
    ->contain(['Categories'])
    ->where([
        "MATCH(Posts.title) AGAINST('{$search}' IN BOOLEAN MODE)" 
    ]);

Upvotes: 6

Related Questions