学说查询 - 忽略空格 [英] Doctrine query - ignoring spaces

查看:19
本文介绍了学说查询 - 忽略空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来创建忽略空格的学说查询.我尝试更换,但我一直收到

I`m looking for a way to create a doctrine query with ignoring spaces. I try with replace but I receive all the time

预期的已知函数,得到替换"

Expected known function, got 'replace'

我的查询看起来像:

        $query = $em->createQueryBuilder();

        $query->select('c')
                ->from('ACMEUserBudnleEntityUser', 'c')
                ->where('replace(c.username," ","")'.' LIKE :searchName')
                ->setParameter('searchName', '%@' . $searchName. '%')
                ->orderBy('c.username', 'asc');

推荐答案

好的,我写了一个替换 DQL 函数.

Ok I write a replace DQL Function.

<?php

namespace AcmeUserBundleDQL;

use DoctrineORMQueryLexer; 
use DoctrineORMQueryASTFunctionsFunctionNode; 


/**
 * "REPLACE" "(" StringPrimary "," StringSecondary "," StringThird ")"
 */
class replaceFunction extends FunctionNode{

    public $stringFirst; 
    public $stringSecond; 
    public $stringThird; 


    public function getSql(DoctrineORMQuerySqlWalker $sqlWalker) {
        return  'replace('.$this->stringFirst->dispatch($sqlWalker) .','
                . $this->stringSecond->dispatch($sqlWalker) . ',' 
                .$this->stringThird->dispatch($sqlWalker) . ')';
    }

    public function parse(DoctrineORMQueryParser $parser) {

        $parser->match(Lexer::T_IDENTIFIER);
        $parser->match(Lexer::T_OPEN_PARENTHESIS);
        $this->stringFirst = $parser->StringPrimary();
        $parser->match(Lexer::T_COMMA);
        $this->stringSecond = $parser->StringPrimary();
        $parser->match(Lexer::T_COMMA);
        $this->stringThird = $parser->StringPrimary();
        $parser->match(Lexer::T_CLOSE_PARENTHESIS);
    }

}

接下来在 app/config.yml 中添加:

Next in app/config.yml I add:

doctrine:
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true
        dql:
            string_functions:
                replace: AcmeUserBundleDQL
eplaceFunction

最后我在我的控制器中创建了一个 DQL 查询:

And finally I create a DQL query in my Controller:

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

    $query = $em->createQueryBuilder();

    $query->select('u')
            ->from('AcmeUserBundleEntityUser', 'u')

            ->where("replace(u.username,' ','') LIKE replace(:username,' ','') ")
            ->setParameter('username', '%' . $usernameForm . '%')
            ->orderBy('u.username', 'asc');


    $result = $query->getQuery()->getResult();

最有趣的是引号"非常重要.这意味着您可以看到在 select、from、setParameter 和 orderBy 中我使用了 '' 但在我使用"和空格的地方.相反是行不通的.我不知道为什么.

The most funny thing is that "quotes" are very important. It means that you can see that in select, from, setParameter and orderBy I use '' but in where I use "" and space ''. The opposite is not working. I don`t know why.

这篇关于学说查询 - 忽略空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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