[英]Symfony2 Doctrine: form to retrieve data by date range
我正在嘗試使用通過日期范圍查詢數據的表單從數據庫中檢索。 我的設置如下:
在我的控制器中:
/**
* @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標題,整個頁面空白。 我搞砸了查詢嗎?
將'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.