Symfony 带LPAD的条令查询(自定义函数)

Symfony 带LPAD的条令查询(自定义函数),symfony,doctrine,Symfony,Doctrine,我需要在Doctrine中执行下一个查询: SELECT MAX(LPAD(SUBSTRING(code, 1, instr(code, '/')-1), 6, '0')) as lastCode FROM provider WHERE entry_date >= '%s'", date("Y") . "-01-01" 简而言之:我有代码为xx/yy的提供者,其中yy为2位年份,xx为序列号,由于此代码为varchar,我执行查询以获取今年的最后一个数字代码 问题在于LPAD的使用。如果

我需要在Doctrine中执行下一个查询:

SELECT MAX(LPAD(SUBSTRING(code, 1, instr(code, '/')-1), 6, '0')) as lastCode
FROM provider
WHERE entry_date >= '%s'", date("Y") . "-01-01"
简而言之:我有代码为xx/yy的提供者,其中yy为2位年份,xx为序列号,由于此代码为varchar,我执行查询以获取今年的最后一个数字代码

问题在于LPAD的使用。如果我使用查询生成器,我会在使用自定义函数时出错,如果我使用本机查询,我会得到空结果

这是我的密码:

使用本机查询:

使用查询生成器:

查询生成器引发以下错误:

错误:预期为已知函数,获得“LPAD”

我还有其他选择吗?为什么本机查询返回空结果?mysql控制台上的相同查询显示最后一个数字


注意:我试过CASTMAXSUBSTRING。。。作为未签名的。。。但是CAST函数也有同样的问题

我在本机查询中出错,如果我正确配置ResultsMapping本机查询,则可以正确处理自定义函数:

$rsm = new ResultSetMapping();
$rsm->addScalarResult('lastCode', 'lastCode');

$sqlMaxCode = sprintf("SELECT MAX(LPAD(SUBSTRING(code, 1, instr(code, '/')-1), 6, '0')) as lastCode
    FROM provider
    WHERE entry_date >= '%s'", date("Y") . "-01-01");

$nQuery = $em->createNativeQuery($sqlMaxCode, $rsm);
$rows = $nQuery->getSingleResult();
$this->get('logger')->info(print_r($rows, true));

谢谢你的回答。
$qb = $em->getRepository('AppBundle:Provider')->createQueryBuilder('I');
$qb->select('MAX(LPAD(SUBSTRING(code, 1, instr(code, "/")-1), 6, "0")) as lastCode')
    ->where("I.entryDate >= ?1")
    ->setParameter('1', date("Y") . "-01-01");

$result = $qb->getQuery()->getSingleResult();
$rsm = new ResultSetMapping();
$rsm->addScalarResult('lastCode', 'lastCode');

$sqlMaxCode = sprintf("SELECT MAX(LPAD(SUBSTRING(code, 1, instr(code, '/')-1), 6, '0')) as lastCode
    FROM provider
    WHERE entry_date >= '%s'", date("Y") . "-01-01");

$nQuery = $em->createNativeQuery($sqlMaxCode, $rsm);
$rows = $nQuery->getSingleResult();
$this->get('logger')->info(print_r($rows, true));