Symfony 控制器中未定义的变量

Symfony 控制器中未定义的变量,symfony,Symfony,我在存储库中有une dql查询: public function TabloBordCmdPeriod($dat1,$dat2) {$qb = $this->createQueryBuilder('a') ->join('a.comElem','c') ->select("sum(c.total) as total") ->where("a.etat = 'Termine'") ->andwhere('a.dateCreation

我在存储库中有une dql查询:

public function TabloBordCmdPeriod($dat1,$dat2)
{$qb = $this->createQueryBuilder('a')
    ->join('a.comElem','c')
    ->select("sum(c.total) as total")
    ->where("a.etat = 'Termine'")
    ->andwhere('a.dateCreation between :dat1 AND :dat2')
    ->setParameters(array('dat1'=>$dat1,'dat2'=>$dat2));
 $query = $qb->getQuery();
$resultats = $query->getSingleScalarResult();
 return $resultats;}
查询运行良好

我在没有类的搜索表单中使用此查询,以下是代码表单:

class TBRechPeriodType extends AbstractType {
public function buildForm(FormBuilder $builder, array $options){
$builder
->add('du','date',array('required'=>false,'widget' => 'single_text',  'format' => 'dd-MM-yyyy', 'attr' => array('class' => 'date input-medium')))
->add('au','date',array('required'=>false,'widget' => 'single_text',  'format' => 'dd-MM-yyyy', 'attr' => array('class' => 'date input-medium')))
;
}
public function getName(){
return 'Recherche_Periodique';}
}
在控制器中,代码为:

public function TBAction()
{
    $formTBrech=$this->createForm(new TBRechPeriodType());
    $em = $this->getDoctrine()->getEntityManager();
    if( $request->getMethod() == 'POST')
    {
        $formTBrech->bindRequest($request);
        if( $formTBrech->isValid() )
        {   $data = $formTBrech->getData();
             $period = $em->getRepository('ZXGescodBundle:Commande')->TabloBordCmdPeriod($data['du'],$data['au']);
        }
    }       
      $entities = $em->getRepository('ZXGescodBundle:Commande')->findall();
     return $this->render('ZXGescodBundle:Commande:TB.html.twig', array(
        'formTBrech' => $formTBrech->createView(),
        'period'      => $period,
        ));
}
加载模板时,我收到一条错误消息:

Notice: Undefined variable: period in C:\wamp\www\elitetest\src\ZX\GescodBundle\Controller\CommandeController.php line...
如何解决?
如果此操作未返回任何结果,请提前感谢

$em->getRepository('ZXGescodBundle:Commande')->TabloBordCmdPeriod($data['du'],$data['au']);
变量$period未指定值。 所以在分配任务之前测试它,或者按照Leggendario的建议去做

编辑

可能您的表单无效,因此它会跳过为$period赋值的部分

if( $formTBrech->isValid() )
        {  error_log("form is valid"); $data = $formTBrech->getData();
            $period = $em->getRepository('ZXGescodBundle:Commande')->TabloBordCmdPeriod($data['du'],$data['au']);

        return $this->render('ZXGescodBundle:Commande:TB.html.twig', array(
           'formTBrech' => $formTBrech->createView(),
           'period'      => $period,
        ));
    } else {   error_log("form not valid");   ... }

'period'=>isset$period$period:null事实上,我通过一个表单动态地进行搜索查询,有两个参数date1和date2,我测试了$variable period,正如leggendario所述,我仍然有相同的错误。