3

我在 MySQL 中使用 Doctrine 2(更准确地说是 Doctrine ORM 2.1)并希望找到以数字开头的条目。使用本机 SQL,我会编写如下内容:

SELECT * FROM table WHERE name RLIKE '^[0-9]';

但是,当我尝试使用 DQL 执行此操作时,出现以下错误:

[Syntax Error] line 0, col 71: Error: Expected =, <, <=, <>, >, >=, !=, got 'RLIKE'
#0 .../Doctrine/ORM/Query/Parser.php(396): Doctrine/ORM/Query/QueryException::syntaxError('line 0, col 73:...')
#1 .../Doctrine/ORM/Query/Parser.php(2746): Doctrine/ORM/Query/Parser->syntaxError('=, <, <=, <>, >...')
#2 .../Doctrine/ORM/Query/Parser.php(2533): Doctrine/ORM/Query/Parser->ComparisonOperator()
#3 .../Doctrine/ORM/Query/Parser.php(2071): Doctrine/ORM/Query/Parser->ComparisonExpression()
#4 .../Doctrine/ORM/Query/Parser.php(1994): Doctrine/ORM/Query/Parser->SimpleConditionalExpression()
#5 .../Doctrine/ORM/Query/Parser.php(1953): Doctrine/ORM/Query/Parser->ConditionalPrimary()
#6 .../Doctrine/ORM/Query/Parser.php(1927): Doctrine/ORM/Query/Parser->ConditionalFactor()
#7 .../Doctrine/ORM/Query/Parser.php(1899): Doctrine/ORM/Query/Parser->ConditionalTerm()
#8 .../Doctrine/ORM/Query/Parser.php(1218): Doctrine/ORM/Query/Parser->ConditionalExpression()
#9 .../Doctrine/ORM/Query/Parser.php(769): Doctrine/ORM/Query/Parser->WhereClause()
#10 .../Doctrine/ORM/Query/Parser.php(738): Doctrine/ORM/Query/Parser->SelectStatement()
#11 .../Doctrine/ORM/Query/Parser.php(210): Doctrine/ORM/Query/Parser->QueryLanguage()
#12 .../Doctrine/ORM/Query/Parser.php(281): Doctrine/ORM/Query/Parser->getAST()
#13 .../Doctrine/ORM/Query.php(203): Doctrine/ORM/Query/Parser->parse()
#14 .../Doctrine/ORM/Query.php(223): Doctrine/ORM/Query->_parse()
#15 .../Doctrine/ORM/AbstractQuery.php(586): Doctrine/ORM/Query->_doExecute()
#16 .../Doctrine/ORM/AbstractQuery.php(392): Doctrine/ORM/AbstractQuery->execute(Array, 1)

我尝试的是:

$regex = '^[0-9]';
$em
    ->createQuery('
        SELECT t
        FROM MyEntity t
        WHERE t.visible = true 
            AND t.name RLIKE ?1
        ORDER BY t.name ASC')
    ->setParameter(1, $regex)
    ->getResult();

然后:

$regex = '^[0-9]';
$em
    ->createQueryBuilder()
    ->select('t')
    ->from('MyEntity', 't')
    ->where('t.visible = true')
    ->andWhere(new Doctrine\ORM\Query\Expr\Comparison('t.name', 'RLIKE', '?1'))
    ->orderBy('t.name', 'ASC')
    ->getQuery()
    ->setParameter(1, $regex)
    ->getResult();

您知道如何在 DQL 中使用 RLIKE 吗?

此致 ;)

4

1 回答 1

1

好问题,不幸的是我没有一个好的答案。

唯一想到的是创建自己的 DQL 函数,如文档中所述。祝你好运;)

也看看这个问题,也许它会有所帮助。

如果您解决了此问题,请发布答案。

于 2011-12-19T11:41:18.963 回答