简体   繁体   English

如何使用Doctrine在多对多关系中找到给定值?

[英]How to find a given value in a many-to-many relationship using Doctrine?

I have an entity "Discussion" that has (among other things): 我有一个实体“讨论”,该讨论有(除其他事项外):

  • A category property (every discussion has 1 category associated with it) 类别属性(每个讨论都有一个关联的类别)
  • A ManyToMany association of "recommended categories" (every discussion can be recommended in multiple categories) “推荐类别”的ManyToMany关联(可以在多个类别中推荐每次讨论)

I'm trying to select all the discussions that are either in a specific category (their category property is that category) or that are recommended in this specific category. 我试图选择属于特定类别(其类别属性是该类别)或此特定类别中推荐的所有讨论。

I can easily get all the discussions that are in this category like this: 我可以轻松获得该类别中的所有讨论,例如:

 $qb = self::$em->getRepository('Discussion')->createQueryBuilder('d');
 $discussions = $qb->where('d.category='.$current_category_id)
 ->setFirstResult( $offset )
 ->setMaxResults( $limit )          
 ->getQuery()->getResult();

I need to add to that all the discussions that have current_category_id in their list of recommended_categories. 我需要补充的是,所有在“ recommended_categories”列表中包含“ current_category_id”的讨论。

So adding something like this: 因此添加如下内容:

$qb = self::$em->getRepository('Discussion')->createQueryBuilder('d');
$discussions = $qb->where('d.category='.$current_category_id)

  ->orWhere($qb->expr()->in($current_category_id, 'd.recommended_categories'))

 ->setFirstResult( $offset )
 ->setMaxResults( $limit )          
 ->getQuery()->getResult();

Which gives an SQL error because it doesn't like the "4 IN(d.recommended_categories))" in the SQL. 由于它不喜欢SQL中的“ 4 IN(d.recommended_categories))”,因此产生了SQL错误。

This is what the recommended_categories looks like in the Discussions entity: 这是在“讨论”实体中的recommended_categories外观:

/**
 * @ManyToMany(targetEntity="Category")
 * @JoinTable(name="discussion_recommended_categories",
 *   joinColumns={@JoinColumn(name="discussion_id", referencedColumnName="id")},
 *   inverseJoinColumns={@JoinColumn(name="category_id", referencedColumnName="id")}
 * )
 */
private $recommended_categories;

And here is the Category entity: 这是类别实体:

/** @Id @Column(type="integer") @GeneratedValue **/
protected $id;

/** @Column(type="string", unique=true, nullable=false,name="Name") **/
protected $name;

I also tried playing around with exists (does $current_category_id exist in d.recommended_categories) but nothing worked. 我也尝试过使用exist(d.recommended_categories中是否存在$ current_category_id),但没有任何效果。

Any ideas how I can check if a given value (not a column) exists in a list of values associated with that entity? 有什么想法可以检查给定值(而不是列)是否存在于与该实体相关联的值列表中?

Thanks! 谢谢!

First of all you should use prepared statements for the parameter in your where clause so you are protected against sql injection. 首先,您应该在where子句中为参数使用准备好的语句 ,以便防止sql注入。 Also you need to pass in your current category entity and Doctrine will magically extract the id from it by looking at your mapping. 另外,您需要传递当前的类别实体,Doctrine会通过查看您的映射神奇地从中提取ID。 Assuming you have $currentCategory available. 假设您有$currentCategory可用。

$discussions = $qb
    ->where('d.category = :category')
    ->setParameter('category', $currentCategory);

You are trying to use a IN to check for a given value in a collection, but this is not what IN is meant for. 您正在尝试使用IN来检查集合中的给定值,但这不是IN的意思。 You should use this for a one of condition (as a replacement for several or's). 您应该将其用于以下one of情况(作为多个条件的替代)。 ie myfield = 1 OR myfield = 2 OR myfield = 3 you can write as myfield IN (1, 2, 3). 即myfield = 1或myfield = 2或myfield = 3可以写成myfield IN(1、2、3)。 I think you got a syntax error because you have the argument order wrong. 我认为您语法错误,因为参数顺序错误。

Anyway get rid of the IN and just add a join to the recommended_categories and a orWhere on the category. 无论如何,要摆脱IN ,只需在orWhere和类别中的orWhere添加orWhere All together you should end up something like this.. untested. 总之,您应该最终得到这样的结果..未经测试。

$discussions = $qb
    ->where('d.category = :category')
    ->leftJoin('d.recommended_categories', 'rc')
    ->orWhere('rc = :category')
    ->setParameter('category', $currentCategory);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM