Search code examples
symfonycontrollerrepositoryquery-builderdql

symfony2 search form querybuilder with parameters


here's a picture of the page on the left i have a form when submitted i want it to display the found vehicles on the right side using the informations entered by the user else if the user does not go to the form the page shows all the cars .in both cases the user should only sees cars where price is not 0

enter image description here

this is my form in case needed but i have no problem 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' ))       

here's my controller :

  $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(),

  ));

i'm getting this error :

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

and this is my querybuilder i think the problem come from here but i don't know what i'm doing wrong exactly

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

Solution

  • The getQuery() method is called before the rest of optionnal expressions...

    Try to "move it" :

    public function myFindAll(FormInterface $filterForm)
    {
         $qb = $this->createQueryBuilder('p');
    
         /***/
    
         return $qb->getQuery()->getResult();
    }