Symfony2 Doctrine:用于按日期范围检索数据的表单

时间:2014-04-22 20:50:33

标签: php symfony doctrine-orm

我尝试使用按日期范围查询数据的表单从数据库中检索。我的设置如下:

在我的控制器中:

 /**
 * @param Request $request
 * @return Response
 * @Route("/invsum", name="reports_invsum")
 * @Method("GET")
 */
public function searchInvSumAction(Request $request)
{
    $form = $this->createFormBuilder()
        ->setMethod('GET')
        ->add('fromdate','date', array(
                'label' => 'From: ',
                'widget' => 'single_text'
            ))
        ->add('todate','date', array(
                'label' => 'To: ',
                'widget' => 'single_text'
            ))
        ->add('generate','submit')
        ->getForm();

    $form->handleRequest($request);
    if($form->isValid()) {

        return $this->forward('CIRBundle:Reports:getInvSum', array(
                'fromdate' => $form->get('fromdate')->getData(),
                'todate' => $form->get('todate')->getData()
            ));
    }
    return $this->render('CIRBundle:Reports:index.html.twig', array(
            'invsum'    => $form->createView(),
        ));
}

/**
 * @param $fromdate
 * @param $todate
 * @return Response
 * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
 */
public function getInvSumAction($fromdate, $todate) {
    $getinvsum = $this->getDoctrine()->getRepository('CIRBundle:SumitomoSub')
                ->findInvSumByDate($fromdate, $todate);

    if(!$getinvsum) {
        throw $this->createNotFoundException('Unable to find Inventory Summary');
    }

    return $this->render('CIRBundle:Reports:invsum.html.twig', array(
            'getinvsum' => $getinvsum
        ));
}

我的查询:

public function findInvSumByDate($fromdate, $todate) {

    $query = $this->getEntityManager()
        ->createQuery('
            SELECT
                m.dano, m.partno, m.batchno
            FROM
              CIRBundle:SumitomoSub s
            JOIN
              s.main m
            WHERE
              m.indate >=  :fromdate and m.indate <= :todate
        ')->setParameter('fromdate', $fromdate)
          ->setParameter('todate', $todate);

     try {
         return $query->getResult();
     } catch(\Doctrine\ORM\NoResultException $e) {
         return null;
     }

}

模板:

<div id="content-box">
     <h2>Inventory Summary </h2>

    {% if invsum is defined %}
        <h1>Results for Batch <span class="searchtitle">{{ invsum.0.batchno }}</span></h1>

        <table class="tablesorter">
            <thead>
            <th>DA</th>
            <th>Part</th>
            <th>Batch</th>
            </thead>
            {% for invsum in invsum %}
                <tr>
                    <td>{{invsum.dano}}</td>
                    <td>{{invsum.partno}}</td>
                    <td>{{invsum.batchno}}</td>
                </tr>
            {% endfor %}
        </table>

    {% endif %}

</div>

当我将变量插入树枝模板时,没有显示任何内容。它只放弃了h1标题,整个页面都是空白的。我搞砸了查询吗?

1 个答案:

答案 0 :(得分:0)

'getinvsum' => $getinvsum更改为'invsum' => $getinvsum,方法如下:

public function getInvSumAction($fromdate, $todate) {
    $getinvsum = $this->getDoctrine()->getRepository('CIRBundle:SumitomoSub')
                ->findInvSumByDate($fromdate, $todate);

    if(!$getinvsum) {
        throw $this->createNotFoundException('Unable to find Inventory Summary');
    }

    return $this->render('CIRBundle:Reports:invsum.html.twig', array(
            'invsum' => $getinvsum
        ));
}