The error is pretty straight forward. I use Symfony 4 and MSSQL Server as a database server with below configuration:
driver: 'sqlsrv'
url: 'mssql://username:password@host:1433/db'
The error happens when I use setFirstResult()
$entityManager->createQueryBuilder()->select('u')->from(UserEntity::class, 'u')
->setFirstResult(20)->setMaxResults(20)
->getQuery()->getArrayResult();
Error message:
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
The solution that works for me is to manually use raw query:
$sql .= ' OFFSET ' . $offset . ' ROWS FETCH NEXT ' . $itemPerPage . ' ROWS ONLY';
But this solution will kill the purpose of using ORM and it will not work if I switch to MySQL.
What is the proper way to solve the error above?