简体   繁体   中英

How To Use MATCH In Symfony2 Database Query

I'm building a search feature for my Symfony2 project, and I wrote the SQL for it as follows:

SELECT dlc.title, dlc.description, dlc.keywords
FROM ShoutMainBundle:Dlc dlc
WHERE MATCH (dlc.title, dlc.description, dlc.keywords) AGAINST (":keyword" IN BOOLEAN MODE)
AND dlc.type = (":audio")
ORDER BY dlc.date DESC

However, when I run this in the project the following error is given:

[Syntax Error] line 0, col 96: Error: Expected known function, got 'MATCH'

Is there an alternative I could use instead of MATCH ? At the moment (just so I can do basic testing) I'm using LIKE, but it doesn't work too well if it's more than one word being used to search with.

EDIT: This is how the code is used within the code:

    $em = $this->getDoctrine()->getEntityManager();

    $wckeyword = '%'.$skeyword.'%';

    $dlcresult = $em->createQuery('
        SELECT dlc.title, dlc.description, dlc.keywords
        FROM ShoutMainBundle:Dlc dlc
        WHERE MATCH (dlc.title, dlc.description, dlc.keywords) AGAINST (":keyword" IN BOOLEAN MODE)
        AND dlc.type = (":audio")
        ORDER BY dlc.date DESC'
    )->setParameters(array('type' => $stype, 'keyword' => $wckeyword));

    $dlcres = $dlcresult->getResult();

Doctrine doesn't support that out-of-the-box, true. But you can:

This is not currently possible with Doctrine2 ORM. As Doctrine supports many different database vendors and most of them don't have a FULLTEXT search feature, it's not supported at all.

You can always use Doctrine2 DBAL for searching. You lose all these nifty orm features, but in my practice they aren't that needed in searching situations anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM