简体   繁体   English

学说:如何使用替换功能

[英]Doctrine : how to use Replace function

I need your help to build my query with Doctrine. 我需要你的帮助来使用Doctrine构建我的查询。 I am a symfony beginner. 我是一个symfony初学者。 Firstly I built my query inside MySQL SQL tab and it's working fine. 首先,我在MySQL SQL选项卡中构建了我的查询,它工作正常。

SELECT * 
FROM contact
WHERE insee like '03%'
ORDER BY (LENGTH(tif) - LENGTH(REPLACE(tif,";",""))) DESC

To be more precise, my tif field looks like that : 
1 - 01.02.01.02;01.02.03.04;01.05.06 (3 subsets)
2 - 01.02.03.08.07.01.02.03.08.0701.02.03.08.07; (1 subset)
3 - 01.02.01;02.06.05 (2 subsets) 

I need to get the number of codes order by desc so as to get the order 1,3,2. 我需要通过desc获取代码的数量,以获得订单1,3,2。

Now I tried to build it in my repository class on Symfony I found out that replace function doesn't exist upon Doctrine so I tried to skirt it by doing what follows : 现在我尝试在Symfony上的我的存储库类中构建它我发现在Doctrine上不存在替换函数所以我试图通过执行以下操作来绕过它:

$qb = $this->getEntityManager()
            ->createQueryBuilder()
            ->select('c')
            ->from('SgaContactBundle:Contact', 'c')
            ->where('c.insee LIKE :insee')
            ->setParameter('insee', '%' . $insee . '%');

$qb->orderBy($qb->expr()->diff(
            $qb->expr()->length('c.tif'), 
            $qb->expr()->length(preg_match_all('/;/i', 'c.tif')) ),
            'DESC');
return $qb->getQuery()
          ->getResult();

Finally I've got this error : 最后我有这个错误:

 [Syntax Error] line 0, col 99: Error: Expected StateFieldPathExpression | string | 
 InputParameter | FunctionsReturningStrings | AggregateExpression, got '0'

What can I do in order to replace "Replace function" ? 我该怎么做才能取代“替换功能”? I tried preg_replace, preg_match and finally preg_match_all but something goes wrong. 我尝试了preg_replace,preg_match,最后是preg_match_all,但出了点问题。

Thanks for your help guys 谢谢你的帮助

Doctrine DQL has no replace function. Doctrine DQL没有替换功能。 But you can implement your own user-defined function. 但是您可以实现自己的用户定义函数。 See https://stackoverflow.com/a/21652988/2015279 请参阅https://stackoverflow.com/a/21652988/2015279

Just a warning : 只是一个警告:

Be careful guys when you are creating your own DQL function, you must create a class with first letter uppercase and but changes upon app/config.yml. 在创建自己的DQL函数时要小心,你必须创建一个首字母大写的类,但在app / config.yml上更改。

I was stuck in it on production mode while I didn't have a problem on dev mode. 我在生产模式下陷入困境,而我在开发模式下没有问题。 But when I changed : 但是当我改变时:

class replaceFunction extends FunctionNode{

by 通过

class ReplaceFunction extends FunctionNode{

and everything works. 一切正常。

Make changes on app/config.yml file as well. 也可以在app / config.yml文件上进行更改。

You can implement your own function like the validate response, OR you can use this Doctrine Extension package that include REPLACE() mysql function and much more (Match against, ROUND, ASIN...) : https://github.com/beberlei/DoctrineExtensions 您可以实现自己的函数,如验证响应,或者您可以使用包含REPLACE()mysql函数的此Doctrine扩展包等等(Match against,ROUND,ASIN ...): https//github.com/beberlei / DoctrineExtensions

Simply install this to your project 只需将其安装到您的项目中即可

composer require beberlei/doctrineextensions

And add the function you want (only) into your config.yml doctrine conf : 并将您想要的功能(仅)添加到config.yml doctrine conf中:

# Doctrine Configuration
doctrine:
    orm:
    entity_managers:
        default:
        #...
            dql:
                string_functions:
                    match: DoctrineExtensions\Query\Mysql\MatchAgainst
                    replace: DoctrineExtensions\Query\Mysql\Replace

Here is a small exemple for REPLACE in query builder : 以下是查询构建器中REPLACE的一个小例子:

$qb = $this->createQueryBuilder('ts')
    ->where("REPLACE(ts.reference, ' ','') LIKE :reference")
    ->setParameter('reference', $reference)
;

Hope it can help someone after 希望它可以帮助别人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM