Hi all! I found the resolved topic about custom dialect https://forum.phalcon.io/discussion/17641/bitcount-not-supported-by-phalcon. I tried the solution there and i've came across with a few questions.

  1. OP code snippet contained BITWISE XOR operator (^) howerver Phalcon does not contain that operator, does it? So I had to create custom function like so:
    $dialect->registerCustomFunction(
        'BITWISE_XOR',
        function($dialect, $expression) {
            return sprintf(
                '%d ^ %d',
                $dialect->getSqlExpression($expression['arguments'][0]),
                $dialect->getSqlExpression($expression['arguments'][1])
            );
        }
    );
  2. The proposed solution by corentin-begne does not seem to work for me. Here is my test. If I do this query in mysql cli:
    SELECT MAX(BIT_COUNT(1222+5666)) from Temp;

    the result is: 7. If i do the same query in phalcon:

    $phql = "SELECT MAX( BIT_COUNT( 1222 + 5666 ) ) from Temp";
    $query = $this->modelsManager->createQuery($phql);
    $r = $query->execute();
    print_r($r->getLast());

    the result is :

    Phalcon\Mvc\Model\Row Object
    (
    [0] => 5
    )

    However if I change custom function sprintf from %d to %s like so:

    $dialect->registerCustomFunction(
            'BIT_COUNT',
            function($dialect, $expression) {
                return sprintf(
                    'BIT_COUNT(%s)',
                    $dialect->getSqlExpression($expression['arguments'][0])
                );
            }
    );

    the result is like it should be:

    Phalcon\Mvc\Model\Row Object
    (
    [0] => 7
    )

Also I tried to specify types for the binding variables like {number:int} but it does not help either.

Help please. Thanks in advance!