-1

I have two fields for Searching, that is, Min and Maximum.

Here, I want to display all the products which are between min and maximum.

Help me I'm new to Symfony.

enter image description here

  • 2
    You have to update your question. Put the code you've tried and maybe we can help, but if there is no question seems like you want that somebody makes your work – Oscar Feb 28 '19 at 12:24

1 Answers1

1

There are two options :

  1. Simple.

    • create ordinary html form in template (that you've already done):
<form>
    <input name="min" value="{{ min }}">
    <input name="max" value="{{ max }}">
    <input type="submit" value="Search">
</form>
  • in your controller :
public function listPhones(Request $request, EntityManagerInterface $em) 
{
    // get submitted values
    $min = $request->get('min');
    $min = $request->get('min');

    $phones = $em->getRepository(Phone::class)->search($min, $max);

    return ['phones' => $phones, 'min' => $min, 'max' => $max]
}
  • in your entity repository:
class PhoneRepository extends EntityRepository 
{
    public function search($min = null, $max = null) 
    {
        $qb = $this->createQueryBuilder('p');
        if (!is_null($min)) {
            $qb->andWhere('p.price >= :min')
               ->setParameter('min', $min);
        }
        if (!is_null($max)) {
            $qb->andWhere('p.price <= :max')
               ->setParameter('max', $max);
        }

        return $qb->getQuery()->getResult();
    }
}
  1. Symfony way, using Symfony Forms. It is a way more complex, I will explain on demand.
Brett Green
  • 3,535
  • 1
  • 22
  • 29
VladRia
  • 1,475
  • 3
  • 20
  • 32