如何在Symfony2数据库查询中使用MATCH [英] How To Use MATCH In Symfony2 Database Query

查看:72
本文介绍了如何在Symfony2数据库查询中使用MATCH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Symfony2项目构建搜索功能,并为此编写了SQL,如下所示:

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:

[语法错误]第0行,列96:错误:得到了预期的已知函数 'MATCH'

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

是否可以代替MATCH使用替代方法?目前(只是我可以进行基本测试),我正在使用LIKE,但是如果要使用多个单词来搜索,效果会不太好.

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.

这是在代码内使用代码的方式:

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();

推荐答案

Doctrine2 ORM当前无法实现.由于Doctrine支持许多不同的数据库供应商,而且大多数供应商没有FULLTEXT搜索功能,因此完全不支持.

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.

您始终可以使用 Doctrine2 DBAL 进行搜索.您会失去所有这些漂亮的orm功能,但是在我的实践中,无论如何,搜索情况都不是必需的.

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.

这篇关于如何在Symfony2数据库查询中使用MATCH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆