教义查询 - 忽略空格 [英] Doctrine query - ignoring spaces

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

问题描述

我正在寻找一种创建无视空格的教义查询的方法。
我尝试替换,但我收到所有的时间

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('ACME\UserBudnle\Entity\User', 'c')
                ->where('replace(c.username," ","")'.' LIKE :searchName')
                ->setParameter('searchName', '%@' . $searchName. '%')
                ->orderBy('c.username', 'asc');


推荐答案

确定我写了一个替换DQL函数。 >

Ok I write a replace DQL Function.

<?php

namespace Acme\UserBundle\DQL;

use Doctrine\ORM\Query\Lexer; 
use Doctrine\ORM\Query\AST\Functions\FunctionNode; 


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

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


    public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker) {
        return  'replace('.$this->stringFirst->dispatch($sqlWalker) .','
                . $this->stringSecond->dispatch($sqlWalker) . ',' 
                .$this->stringThird->dispatch($sqlWalker) . ')';
    }

    public function parse(\Doctrine\ORM\Query\Parser $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);
    }

}

下一个应用/ config.yml我添加:

Next in app/config.yml I add:

doctrine:
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: true
        dql:
            string_functions:
                replace: Acme\UserBundle\DQL\replaceFunction

最后我在我的Controller中创建一个DQL查询:

And finally I create a DQL query in my Controller:

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

    $query = $em->createQueryBuilder();

    $query->select('u')
            ->from('Acme\UserBundle\Entity\User', 'u')

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


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

最有趣的是引号非常重要。这意味着您可以在select,from,setParameter和orderBy中看到,我使用''但是在我使用和space的地方。相反的是没有工作。我不知道为什么

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天全站免登陆