简体   繁体   English

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

[英]Symfony2 Doctrine: form to retrieve data by date range

I'm trying to retrieve from the database using form which queries for the data by date range. 我正在尝试使用通过日期范围查询数据的表单从数据库中检索。 My setup as goes: 我的设置如下:

In my controller: 在我的控制器中:

 /**
 * @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
        ));
}

My query: 我的查询:

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;
     }

}

Template: 模板:

<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>

When I insert the variable into the twig template nothing shows up. 当我将变量插入树枝模板时,什么都没有显示。 It only gave up the h1 Title and the whole page is blank. 它只放弃了h1标题,整个页面空白。 Did I mess up on the query? 我搞砸了查询吗?

Change 'getinvsum' => $getinvsum to 'invsum' => $getinvsum so your method reads: '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
        ));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM