sca*_*amp 3 php mysql doctrine symfony doctrine-orm
我需要你的帮助来使用Doctrine构建我的查询.我是一个symfony初学者.首先,我在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)
Run Code Online (Sandbox Code Playgroud)
我需要通过desc获取代码的数量,以获得订单1,3,2.
现在我尝试在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();
Run Code Online (Sandbox Code Playgroud)
最后我有这个错误:
[Syntax Error] line 0, col 99: Error: Expected StateFieldPathExpression | string |
InputParameter | FunctionsReturningStrings | AggregateExpression, got '0'
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能取代"替换功能"?我尝试了preg_replace,preg_match,最后是preg_match_all,但出了点问题.
谢谢你的帮助
您可以实现自己的功能,如验证响应,或者您可以使用包含 REPLACE() mysql 功能等的 Doctrine Extension 包(匹配、ROUND、ASIN...):https : //github.com/beberlei /教义扩展
只需将其安装到您的项目中
composer require beberlei/doctrineextensions
Run Code Online (Sandbox Code Playgroud)
并将您想要的功能(仅)添加到您的 config.yml 准则 conf 中:
# Doctrine Configuration
doctrine:
orm:
entity_managers:
default:
#...
dql:
string_functions:
match: DoctrineExtensions\Query\Mysql\MatchAgainst
replace: DoctrineExtensions\Query\Mysql\Replace
Run Code Online (Sandbox Code Playgroud)
这是查询构建器中 REPLACE 的一个小例子:
$qb = $this->createQueryBuilder('ts')
->where("REPLACE(ts.reference, ' ','') LIKE :reference")
->setParameter('reference', $reference)
;
Run Code Online (Sandbox Code Playgroud)
希望它可以帮助某人之后
| 归档时间: |
|
| 查看次数: |
8137 次 |
| 最近记录: |