如何在Symfony4中搜索最大值和最小值?

如何在Symfony4中搜索最大值和最小值?,symfony,symfony4,symfony-4.2,Symfony,Symfony4,Symfony 4.2,我有两个搜索字段,即Min和max 在这里,我想显示最小值和最大值之间的所有产品 帮帮我,我是新来的 有两种选择: 简单 在模板中创建普通html表单(已经完成): 在实体存储库中: class PhoneRepository扩展了EntityRepository { 公共函数搜索($min=null,$max=null) { $qb=$this->createQueryBuilder('p'); 如果(!为空($min)){ $qb->andWhere('p.price>=:min')

我有两个搜索字段,即Min和max

在这里,我想显示最小值和最大值之间的所有产品

帮帮我,我是新来的


有两种选择:

  • 简单

    • 在模板中创建普通html表单(已经完成):
    • 在实体存储库中:
    class PhoneRepository扩展了EntityRepository
    {
    公共函数搜索($min=null,$max=null)
    {
    $qb=$this->createQueryBuilder('p');
    如果(!为空($min)){
    $qb->andWhere('p.price>=:min')
    ->setParameter('min',$min);
    }
    如果(!为空($max)){
    
    $qb->andWhere('p.price)您必须更新您的问题。输入您尝试过的代码,也许我们可以提供帮助,但如果没有问题,您似乎希望有人为您的工作提供帮助
    <form>
        <input name="min" value="{{ min }}">
        <input name="max" value="{{ max }}">
        <input type="submit" value="Search">
    </form>
    
    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]
    }
    
    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();
        }
    }