按实体集合过滤。 Symfony2的

时间:2014-04-17 17:16:58

标签: symfony collections filter doctrine-orm

我有这个实体

class Business
{
  ...
  /**
   * 
   * @ORM\ManyToMany(targetEntity="Category", inversedBy="businesses")
   * 
   */
  private $categories;
  ...
}
class Category
{
  ...
  /**
   * 
   * @ORM\ManyToMany(targetEntity="Business", mappedBy="categories")
   * 
   */
  private $businesses;
  ...
}

我在BusinessRepository中尝试此查询但不起作用

$em = $this->getEntityManager();

$dql = "
        SELECT a
        FROM BusinessMainBundle:Business a
        WHERE a.title LIKE :title AND :category IN (a.categories)";

$query = $em->createQuery($dql)
            ->setParameter("title", "%".$title."%")
            ->setParameter("category", $category);

如果你能解决这个问题我会很感激

1 个答案:

答案 0 :(得分:0)

您需要加入类别。您的DQL应如下所示:

    $dql = "
            SELECT a
            FROM BusinessMainBundle:Business:Business a
            LEFT JOIN a.categories c
            WHERE a.title = :title AND c.id IN (:category)
    ";

哪里

    ->setParameter("category", $category);

$ category应该是一个值数组(在此示例中为类别的id)。