我正在尝试从页首横幅中获取一些结果,以便以json格式投放。
我的实际查询非常正常,如下所示:
...
$pos = 25 //some position
$cnx->beginTransaction();
$cnx->executeQuery('SET @rownum := 0');
$sql = "
SELECT * FROM (
SELECT @rownum := @rownum + 1 AS rank, id, name, birthdate, gamesplayed,country,city
FROM board ORDER BY score DESC
) as result
WHERE result.rank >= (" . $pos . "-10) and result.rank <= (" . $pos . "+10)
ORDER BY rank
";
$preresults = $cnx->executeQuery($sql);
$cnx->commit();
$results = $preresults->fetchAll();
该请求有效,但我丢失了所有字段类型(广播),所有值均采用字符串格式。
如何像我使用dql(对象)那样获取适当类型的值?
谢谢
答案 0 :(得分:0)
您需要像这样使用ResultSetMappingBuilder类:
$rsm = new ResultSetMappingBuilder($this->entityManager);
$rsm->addRootEntityFromClassMetadata('App:YourEntity', 'y');
$sql = "SELECT * FROM (
SELECT @rownum := @rownum + 1 AS rank, id, name, birthdate, gamesplayed,country,city
FROM board ORDER BY score DESC
) as result
WHERE result.rank >= (" . $pos . "-10) and result.rank <= (" . $pos . "+10)
ORDER BY rank";
$query = $this->entityManager->createNativeQuery($sql, $rsm);
return $query->getResult();
这将返回一个YourEntity []
数组