Symfony 3 - 名为" getResult"的未定义方法

时间:2016-11-28 14:38:48

标签: symfony

/**
    * Search result information between two dates
    *
*@Route("/search/{startDate}/{endDate}/{type}", name="maintenance_search_result")
    *@Method("GET")
    */
    public function searchResultAction($startDate, $endDate, $type)
    {
      $em = $this->getDoctrine()->getManager()->getRepository('CarMaintenanceBundle:Maintenance');
      $query = $em->createQueryBuilder('c')
        ->select('c.id')
        ->addSelect('c.type')
        ->addSelect('c.date')
        ->addSelect('c.cost')
        ->addSelect('c.remark')
        ->where('c.date > :SDATE')->setParameter('SDATE', $startDate)
        ->andWhere('c.date < :EDATE')->setParameter('EDATE', $endDate);

        if ("All" != $type){
            $query->andWhere('c.type = :TYPE1')->setParameter('TYPE1', $type);
        } 

        $query->orderBy('c.date', 'DESC')
          ->getQuery();

        $SearchRes = $query->getResult();

        return $this->render('maintenance/search_result.html.twig', array(
          'SearchResults'=> $SearchRes,));
}

我有以下错误:

  

尝试调用名为&#34; getResult&#34;的未定义方法。班级   &#34;学说\ ORM \ QueryBuilder的&#34;

当我这样写的时候,没有问题。它显示搜索结果。

public function searchResultAction($startDate, $endDate, $type)
    {
      $em = $this->getDoctrine()->getManager()->getRepository('CarMaintenanceBundle:Maintenance');

        if ("All" != $type){
          $query = $em->createQueryBuilder('c')
            ->select('c.id')
            ->addSelect('c.type')
            ->addSelect('c.date')
            ->addSelect('c.cost')
            ->addSelect('c.remark')
            ->where('c.date > :SDATE')->setParameter('SDATE', $startDate)
            ->andWhere('c.date < :EDATE')->setParameter('EDATE', $endDate)
            ->andWhere('c.type = :TYPE1')->setParameter('TYPE1', $type)
            ->orderBy('c.date', 'DESC')
            ->getQuery();
        } else {
          $query = $em->createQueryBuilder('c')
            ->select('c.id')
            ->addSelect('c.type')
            ->addSelect('c.date')
            ->addSelect('c.cost')
            ->addSelect('c.remark')
            ->where('c.date > :SDATE')->setParameter('SDATE', $startDate)
            ->andWhere('c.date < :EDATE')->setParameter('EDATE', $endDate)
            ->orderBy('c.date', 'DESC')
            ->getQuery();
        }
        $SearchRes = $query->getResult();
        return $this->render('maintenance/search_result.html.twig', array(
          'SearchResults'=> $SearchRes,));
    }

最短代码有什么问题?

1 个答案:

答案 0 :(得分:3)

您没有在任何地方分配getQuery()的结果:

    $query->orderBy('c.date', 'DESC')
      ->getQuery();

这就是诀窍:

    $query = $query->orderBy('c.date', 'DESC')
      ->getQuery();