S
S
senpay12015-10-03 14:41:24
symfony
senpay1, 2015-10-03 14:41:24

Is it possible to somehow use MySQL variables in QueryBuilder or DQL?

In general, I use KnpPaginationBundle for pagination and sorting, the following picture comes out:
1. Option with DQL:

$em = $this->getDoctrine()->getManager();
        $query = $em->createQuery(
            'SELECT a, @C:[email protected] +1 FROM (SELECT @row_number := 0) init, GameServerBundle:Server\Server a
                LEFT JOIN a.map m1
            ORDER BY a.isVip DESC'
        );

DQL does not know how to work with @ (variables) - it throws an error
2. Option with NativeQueryBuilder:
$query2 = $em->createNativeQuery(
            '
            SELECT a.*,
            s2.id AS id_service,
            s2.priority AS `priority`,
            @row_number := @row_number := @row_number + 1  AS `top`

            FROM (SELECT @row_number := 0) init, `server` a
                    LEFT JOIN servers_services ss ON (a.id = ss.server_id)
                    LEFT JOIN service s2 ON (s2.id = ss.service_id)
            WHERE a.is_banned = 0

            ORDER BY ss.service_id IS NULL, priority DESC
            ',
            $rsm
        );

Everything works fine here, ONLY KnpPaginationBundle DOES NOT WORK WITH NativeQuery object... It only accepts an array from NativeQueryResult, and here KnpPaginationBundle does not know how to work with sorting by array.
What to do?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question