Doctrine / MySQL:ResultSetMapping中缺少字段

时间:2013-04-28 22:23:24

标签: php mysql symfony doctrine

我有一个本机查询想要用我的查询结果填充ResultSetMapping对象。 一切都按预期工作,但字段“距离”。结果数组中的字段始终为空。当我直接在MySQL中运行我的查询时,会有“距离”的结果,所以我猜我的SQL没问题?

class PlaceRepository extends EntityRepository
{
    public function getAllWithMainImage($lat, $lon)
    {
        try
        {
            $rsm = new ResultSetMapping;
            $rsm->addScalarResult('slug', 'slug');
            $rsm->addScalarResult('title', 'title');
            $rsm->addScalarResult('rating', 'rating');
            $rsm->addScalarResult('visited_at', 'visited_at');
            $rsm->addScalarResult('img', 'img');
            $rsm->addScalarResult('distance', 'distance');

            return $this->getEntityManager()
                ->createNativeQuery('SELECT p.slug, p.title, p.rating, p.visited_at, pp.file AS img, ROUND(GLength(
                        LineString(
                            p.place_point, 
                            GeomFromText("POINT(:lat :lon)")
                        )
                    ) * 111.12, 1) AS distance
                    FROM place p 
                    LEFT JOIN place_picture AS pp
                    ON p.id = pp.place_id
                    GROUP BY p.id
                    ORDER BY distance
                    LIMIT 20', $rsm)
                ->setParameter('lat', $lat)
                ->setParameter('lon', $lon)
                ->getResult();
        }
        catch(\Doctrine\ORM\NoResultException $e)
        {
            return false;
        }
    }
}

从Crontroller调用的存储库函数:

class PlaceRestController extends Controller
{
    public function getPlacesAction(ParamFetcher $paramFetcher)
    {
        $view = FOSView::create();
        $view->setHeader('Access-Control-Allow-Origin', '*');
        $em = $this->getDoctrine()->getManager();
        $data = $em->getRepository('TouringPlaceBundle:Place')->getAllWithMainImage(48.39227479328352, 9.985520839691162); //hard-coded for debugging
        print_r($data);  //… [img] => xzz.jpg, [distance] =>
        if($data)
        {
            $view->setStatusCode(200)->setData($data);
        }
        return $view;
    }
}

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

您可以在调用此方法的地方添加代码部分吗? 返回值在哪里?在一个实体?

如果我不这么认为,那么问题可能来自倍数引号所以:lat和:lon不会改变成你想要的值。您是否检查过分析器以确定它们已被替换?