如何在Doctrine 2中使用RLIKE?

时间:2011-12-02 15:50:48

标签: php doctrine rlike

我正在使用Doctrine 2(Doctrine ORM 2.1,更准确地说)与MySQL,并希望从数字开始查找条目。使用本机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?

最好的问候;)

1 个答案:

答案 0 :(得分:1)

好问题,遗憾的是我没有一个好的答案。

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

另请查看this question,也许会有所帮助。

如果你解决了这个问题,请发一个答案。