代码之家  ›  专栏  ›  技术社区  ›  vimuth

symfony原则为where子句提供了一个数组

  •  0
  • vimuth  · 技术社区  · 6 年前

    我需要为$qb->和where()添加一个数组。这是我的密码。

    $fuel_type = array("petrol", "diesel", "gasoline");
    $qb = $this->createQueryBuilder('c')
    $qb->andWhere('c.fuelType = :fuel_type')
                ->setParameter('fuel_type', $fuel_type);
    return $qb->getQuery()
                        ->getResult();
    

    我需要检查$fuel_类型值的遍历,并在哪里检查它们。有更简单的方法吗?

    1 回复  |  直到 6 年前
        1
  •  3
  •   ElliotBrl    6 年前

    在你的控制器,服务或者其他的东西里,就这样做。

    $fuel_type = array("petrol", "diesel", "gasoline");
    
    $result = $em->getRepository('App\Entity\Name')->findBy(['fuel_type'=> $fuel_type]);
    

    或者直接在存储库中

    $fuel_type = array("petrol", "diesel", "gasoline");
    
    return $this->createQueryBuilder('c')
            ->where('c.fuelType IN (:val)')
            ->setParameter('val', $fuel_type )
            ->getQuery()
            ->getResult();