2016-11-19 72 views
-1

我遇到了这个查询问题。我想获得只与属性附加伤害$ categoria的车,我这样做:教义和问题

public function listcategoriaAction($categoria) 
    { 
     $posts = $this->get('doctrine')->getManager()->createQueryBuilder()->select('p')->from('CarsCarsBundle:Post', 'p')->where('p.categoria = :categoria')->setParameter('categoria', $categoria)->getQuery()->getResult(); 

     return $this->render('CarsCarsBundle:Cars:list.html.twig', array('posts' => $posts)); 
    } 

但是我收到一个空数组。任何提示将不胜感激

+0

你能告诉如果$ categoria参数是Categoria对象或外键值(即分类的ID)? –

回答

-1

首先,我假设这个代码是在控制器。我强烈建议您避免将查询放在您的控制器上,而不是use repositories

我觉得这个错误发生是因为你以前没有水合你收到的参数类别ID。这将做伎俩:

$dm = $this->get('doctrine')->getManager(); 

//This gets the object from db 
$category = $dm->getRepository('CarsCarsBundle:Category')->findOneById($categoria); 

if ($category !== null) { 

    $posts = $dm->getRepository('CarsCarsBundle:Post')->findOneByCategory($category); 

} else { 

    //The id received is not on the db. 
}