symfony2搜索表单querybuilder与参数

时间:2017-04-15 22:19:32

标签: symfony controller repository query-builder dql

这里是左边页面的图片我提交了一个表单我希望它使用用户输入的信息在右侧显示找到的车辆如果用户没有去表格页面显示所有汽车。在这两种情况下,用户应该只看到价格不是0的汽车

enter image description here

这是我需要的表格,但我在这里没有问题VoitureType3.php:

$builder
->add('marque', EntityType::class,array(
        'class' => 'SpoiledCarFrontOfficeBundle:Marque',
        'required'      => true, 
        'empty_value'   => '== Choisissez une Marque ==',
         ))

       ->add('modele', DependentFormsType::class,array(
       'entity_alias'   => 'modele_by_marque',
       'parent_field'   => 'marque',
       'empty_value'    => '== Choisissez un Modele =='


         ))
    ->add('chevaux', 'choice', array( 'choices' => array('4' => '4', '5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', '10' => '10', '11' => '11', '12' => '12', '13' => '13', '14' => '14', '15' => '15', '16' => '16'),'expanded' => false, 'required' => true,
     'multiple' => false,   'label' => 'Nombre de Chevaux' )) 
    ->add('boitevitesse', 'choice', array( 'choices' => array('Automatique' => 'Automatique', 'Manuelle' => 'Manuelle', 'Sequentielle' => 'Sequentielle'),'expanded' => false, 'required' => true,
     'multiple' => false,   'label' => 'Boite de Vitesse' ))       

这是我的控制人员:

  $filterForm = $this->createForm('SpoiledCar\FrontOfficeBundle\Form\VoitureType3');

 $em = $this->getDoctrine()->getManager();
  $voiturefind = $em->getRepository('SpoiledCarFrontOfficeBundle:Voiture')->myFindAll($filterForm);
 $voitures  = $this->get('knp_paginator')->paginate($voiturefind,$this->get('request')->query->get('page', 1),6); 

// Bind values from the request
$filterForm->handleRequest($request);

 if ($filterForm->isSubmitted() && $filterForm->isValid()) {
  return $this->render('FOSUserBundle:Profile:listTable.html.twig', array(
        'voitures' => $voitures,


 ));
   }
   return $this->render('FOSUserBundle:Profile:listTable.html.twig', array(
        'voitures' => $voitures,
        'filterForm' => $filterForm->createView(),

  ));

我收到此错误:

  Attempted to call an undefined method named "andWhere" of class "Doctrine\ORM\Query". 

这是我的查询工具我认为问题来自这里,但我不知道我做错了什么

public function myFindAll(FormInterface $filterForm)
{

  $qb = $this->createQueryBuilder('p')
    ->Where('p.prix IS NOT NULL')
    ->getQuery();
 if ($filterForm->has('marque')) {
 $qb->andWhere( $qb->expr()->like('u.marque', ':marque'))
  ->setParameter('marque', '%'. $filterForm->get('marque')->getData().'%');
  }
  if ($filterForm->has('modele')) {
 $qb->andWhere( $qb->expr()->like('u.modele', ':modele'))
 ->setParameter('modele', '%'. $filterForm->get('modele')->getData().'%');
  }

 if ($filterForm->has('boitevitesse')) {
 $qb->andWhere( $qb->expr()->like('u.boitevitesse', ':boitevitesse'))
->setParameter('boitevitesse', '%'. $filterForm->get('boitevitesse')- >getData().'%');
  }

 if ($filterForm->has('chevaux')) {
$qb->andWhere( $qb->expr()->like('u.chevaux', ':chevaux'))
->setParameter('chevaux', '%'. $filterForm->get('chevaux')->getData().'%');
}

return $qb->getResult();
}

1 个答案:

答案 0 :(得分:1)

getQuery()方法在其余的optionnal表达式之前调用...

尝试"移动它" :

if($('body').hasClass(no-touch) & $(window).width() > 992) {