php - Doctrine\DBAL\DBALException:平台 mssql 不支持限制查询中的偏移值
问题描述
错误非常简单。我使用 Symfony 4 和 MSSQL Server 作为数据库服务器,配置如下:
driver: 'sqlsrv'
url: 'mssql://username:password@host:1433/db'
当我使用 setFirstResult() 时发生错误
$entityManager->createQueryBuilder()->select('u')->from(UserEntity::class, 'u')
->setFirstResult(20)->setMaxResults(20)
->getQuery()->getArrayResult();
错误信息:
In AbstractPlatform.php line 3359:
[Doctrine\DBAL\DBALException]
Platform mssql does not support offset values in limit queries.
痕迹:
() at /var/www/html/ple-studio/vendor/doctrine/dbal/lib/Doctrine/DBAL/Platforms/AbstractPlatform.php:3359
Doctrine\DBAL\Platforms\AbstractPlatform->modifyLimitQuery() at /var/www/html/ple-studio/vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php:545
Doctrine\ORM\Query\SqlWalker->walkSelectStatement() at /var/www/html/ple-studio/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Exec/SingleSelectExecutor.php:42
Doctrine\ORM\Query\Exec\SingleSelectExecutor->__construct() at /var/www/html/ple-studio/vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php:278
Doctrine\ORM\Query\SqlWalker->getExecutor() at /var/www/html/ple-studio/vendor/doctrine/orm/lib/Doctrine/ORM/Query/Parser.php:398
Doctrine\ORM\Query\Parser->parse() at /var/www/html/ple-studio/vendor/doctrine/orm/lib/Doctrine/ORM/Query.php:283
Doctrine\ORM\Query->_parse() at /var/www/html/ple-studio/vendor/doctrine/orm/lib/Doctrine/ORM/Query.php:295
Doctrine\ORM\Query->_doExecute() at /var/www/html/ple-studio/vendor/doctrine/orm/lib/Doctrine/ORM/AbstractQuery.php:967
对我有用的解决方案是手动使用原始查询:
$sql .= ' OFFSET ' . $offset . ' ROWS FETCH NEXT ' . $itemPerPage . ' ROWS ONLY';
但是这个解决方案会破坏使用 ORM 的目的,如果我切换到 MySQL,它将无法工作。
解决上述错误的正确方法是什么?
解决方案
看起来您可能需要更改配置驱动程序值,如下所示:
driver: 'mssql'
推荐阅读
- python - Pandas 基于 timedelta 的条件外连接(merge_asof)
- jquery - ScrollMagic 动态地将每个面板的wipeAnimation 加倍
- php - 通过php在html标签中插入Php代码
- android-studio - 将 Android Studio 中的深色主题应用于每个活动
- sql-server - 如何正确将导入的 SQL 查询转换为 SSRS 中的表达式字符串
- c# - StyleCop 6.1.0 标记为语法错误 SA102 C# 6 空条件“?” 操作员
- javascript - 什么??JS中的意思
- excel - 如何让我的范围参考配置表上的参数?
- python-3.x - 字符串中的字节到字节
- r - 使用逗号分隔的标签列表拆分数据框列