我想在SearchResult中计算独特的$ hotel。我在视图中传递新变量。
类别:
*
*
* @ORM\Entity(repositoryClass="Uts\HotelBundle\Repository\SearchResult")
* @ORM\Table(name="search_result")
*/
class SearchResult
{
------
/**
* @ORM\ManyToOne(targetEntity="SearchRequest")
* @var
*/
private $request;
/**
* @ORM\ManyToOne(targetEntity="Hotel")
* @var Hotel
*/
private $hotel;
---
}
将变量传递给视图:
$query2 = $repository->hotelsCount($searchId);
// var_dump($query2);
$templateVars['count']=$query2;
}
return $this->render('UtsHotelBundle:Default:results.html.twig', $templateVars)
功能酒店数量:
public function hotelsCount($requestId){
$qb = $this->createQueryBuilder('self');
$qb->select('count DISTINCT(p.hotel)');
$qb->from('HotelBundle:SearchResult','p')
->where('p.request_id = :id')
->setParameter('id',$requestId);
$count = $qb->getQuery()->getSingleScalarResult();
}
答案 0 :(得分:0)
我会做一个像
这样更可重复的查询public function getHotelsByRequest($requestId){
$qb = $this->createQueryBuilder('self');
$qb
->where('self.request_id = :id')
->setParameter('id',$requestId);
return $qb->getQuery()->getResult();
}
然后在您的控制器中
$hotels = $repository->getHotelsByRequest($searchId);
$templateVars['count']= count($hotels);