Search code examples
postgresqldoctrine-ormdql

Cast integer to decimal in DQL


I use Doctrine with a Postgres database and want to update the integer field "voting". It's a procentual value, saved as integer between 0 and 100, based on the two integer fields "voteCountPro" and "voteCount". I have to cast one of the integers to a decimal value. (See: Division ( / ) not giving my answer in postgresql) This doesn't work in DQL and fails with the message:

[Syntax Error] line 0, col 363: Error: Expected Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS, got ':'

UPDATE statement s
SET s.voting = (s.voteCountPro::decimal / s.voteCount) * 100
WHERE s.id = :id

How can I set the value?


Solution

  • Install https://github.com/oroinc/doctrine-extensions, register the CAST function and write:

    UPDATE statement s
    SET s.voting = (CAST(s.voteCountPro as decimal) / s.voteCount) * 100
    WHERE s.id = :id