Doctrine2的查询构建器子字符串辅助方法的正确语法 [英] Correct syntax for Doctrine2's query builder substring helper method

查看:84
本文介绍了Doctrine2的查询构建器子字符串辅助方法的正确语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这个问题可能是由于我对查询构建器帮助程序的模糊理解,但对于我的生活,我找不到使用substring方法的正确方法。

Ok, this problem is probably due to my foggy understanding of the query builder helper methods but for the life of me I cannot find the correct way to use the substring method.

我正在尝试返回以指定的字母数字值开头的所有结果。以下代码不会抛出任何错误,但也不会返回任何结果。我已经挖了谷歌,但显然几乎没有显示如何使用子字符串。我想让这个在查询构建器中工作,但是我可能需要去DQL或者是raw sql。

I am attempting to return all results that begin with a specified alphanumeric value. The below code does not throw any errors but it also does not return any results. I have scoured google but apparently there is almost nothing showing how to use substring. I would like to get this working in query builder but I may have to go DQL or raw sql.

    $qb->select('p', 't');
    $qb->from('ContentParent', 'p');
    $qb->join('p.titleCurrent', 't');
    $qb->where(
            $qb->expr()->eq($qb->expr()->substring('t.sortTitle', 0, 1), ':letter')
        );

谢谢!

推荐答案

SUBSTRING需要以一个为基础。

SUBSTRING needs to be one-based.

MySQL手册指出:


对于所有形式的SUBSTRING()
中第一个字符的位置是要从中提取子串的字符串,计算为
1。

For all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1.

使用 Oracle

SELECT SUBSTR('ABCDEFG',3,4) "Substring"
     FROM DUAL;

Substring
---------
CDEF

Transact-SQL手册

SELECT LastName, SUBSTRING(FirstName, 1, 1) AS Initial
FROM Person.Person
WHERE LastName like 'Barl%'
ORDER BY LastName

从这些示例中可以看出,他们也使用one- (Oracle,但是,当给定 0 )时,Oracle将承担 1

You can see from these examples that they, too, are using one-based (Oracle, however, will assume 1 when given 0).

只需将子串参数更改为't.sortTitle',1,1 即可正确。

Simply change your substring parameters to 't.sortTitle', 1, 1 and it will work correctly.

这篇关于Doctrine2的查询构建器子字符串辅助方法的正确语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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