如何在搜索后使用数组重新渲染模板?

时间:2016-01-26 09:56:25

标签: symfony

我已经在symfony中创建了搜索功能,它有效吗

public function forSaleAction(Request $request)
    {
        $data = array();
        $form = $this->createFormBuilder($data)
            ->add('searchText')

            ->getForm();
        $form->handleRequest($request);


        $ads = $this->getDoctrine()
            ->getRepository('ParkResortBundle:Ad')
            ->findAll();



        if ($form->isSubmitted()) {
            $data = $form->getData();
            $SearchKeyword = $data['searchText'];
            $qb = $this->getDoctrine()->getEntityManager()->getRepository('ParkResortBundle:Ad')->createQueryBuilder( 'p' );
            $qb->add( 'where',
                $qb->expr()->orX(
                    $qb->expr()->like( 'p.adTitle', "'%{$SearchKeyword}%'" ),
                    $qb->expr()->like( 'p.address', "'%{$SearchKeyword}%'" )

                )

            )
               ->addOrderBy('p.price', 'ASC');
            $ads = $qb->getQuery()->getResult();


        }


        if ($request->isXmlHttpRequest()) {
            return $this->render('ParkResortBundle:Pages:_ad.html.twig',
                array('ads' => $ads));
        }


        $paginator = $this->get('knp_paginator');
        $paginated = $paginator->paginate(
            $ads, /* query NOT result */
            $request->query->getInt('page', 1)/*page number*/,
            2/*limit per page*/
        );


        return $this->render('ParkResortBundle:Pages:forSale.html.twig',
            array('ads' => $paginated,
                  'form'=>$form->createView()));
    }

我想在搜索区域为空时(在用户按退格键或全部删除后)列表应该全部显示在for_sale twig模板中。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要将实体管理器与findAll()

一起使用
$ads = $this->getDoctrine()->getEntityManager()
            ->getRepository('ParkResortBundle:Ad')
            ->findAll();