简体   繁体   English

Doctrine 2 无法识别 SUBSTRING_INDEX

[英]SUBSTRING_INDEX not recognised in Doctrine 2

I'm working on a Symfony 3 CRM, and I need to be able to create a list of customers which can be filtered by the first letter of the surname.我正在开发一个 Symfony 3 CRM,我需要能够创建一个客户列表,可以通过姓氏的第一个字母进行过滤。 The issue is, I need to split the column data first in order to find the surname - this is because the database is synced with Sage which requires data in a specific format so I cannot change it.问题是,我需要先拆分列数据才能找到姓氏 - 这是因为数据库与 Sage 同步,后者需要特定格式的数据,因此我无法更改它。

I discovered the SUBSTRING_INDEX function and tried it out in my mySQL software and it works perfectly - so I tried to incorporate it into a DQL query like so:我发现了SUBSTRING_INDEX函数并在我的 mySQL 软件中试用了它,它运行良好 - 所以我尝试将它合并到一个 DQL 查询中,如下所示:

$query = $em->createQuery("SELECT u FROM AppBundle:Customer c WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(c.contact_name, ' ', 3), ' ', -1) LIKE :letter");
$query->setParameter('letter','%'.$letter.'%');

Where $letter is passed via a request from the CRM. $letter是通过来自 CRM 的请求传递的。 However, I get the following error from Symfony when attempting to run:但是,当我尝试运行时,我从 Symfony 收到以下错误:

[Syntax Error] line 0, col 41: Error: Expected known function, got 'SUBSTRING_INDEX' [语法错误] 第 0 行,第 41 列:错误:预期已知函数,得到“SUBSTRING_INDEX”

My guess is that Doctrine does not recognise the SUBSTRING_INDEX function?我的猜测是 Doctrine 无法识别 SUBSTRING_INDEX 函数? Is there an alternative way of doing what I need to, or a way to allow Doctrine to recognise this function?有没有其他方法可以做我需要做的事情,或者有什么方法可以让 Doctrine 识别这个功能?

Doctrine doesn't recognize SUBSTRING_INDEX as a function. Doctrine 不会将 SUBSTRING_INDEX 识别为函数。

You can import a Doctrine Extension to recognize it or change your query for a thing lke this sql LOWER(SUBSTRING(c.contact_name, LOCATE(' ', c.contact_name)-1))你可以导入一个Doctrine Extension来识别它或者改变你的查询, sql LOWER(SUBSTRING(c.contact_name, LOCATE(' ', c.contact_name)-1))这个sql LOWER(SUBSTRING(c.contact_name, LOCATE(' ', c.contact_name)-1))

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

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