错误非常直接。我将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.
Trace:
() 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,它将无法使用。
如何解决上面的错误的正确方法?
错误非常直接。我将Symfony 4和MSSQL Server用作具有以下配置的数据库服务器:驱动程序:'sqlsrv'网址:'mssql:// username:password @ host:1433 / db'错误...
看来您可能需要按如下方式更改配置驱动程序值: