Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find a given value in a many-to-many relationship using Doctrine?
    primarykey
    data
    text
    <p>I have an entity "Discussion" that has (among other things):</p> <ul> <li>A category property (every discussion has 1 category associated with it)</li> <li>A ManyToMany association of "recommended categories" (every discussion can be recommended in multiple categories)</li> </ul> <p>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.</p> <p>I can easily get all the discussions that are in this category like this:</p> <pre><code> $qb = self::$em-&gt;getRepository('Discussion')-&gt;createQueryBuilder('d'); $discussions = $qb-&gt;where('d.category='.$current_category_id) -&gt;setFirstResult( $offset ) -&gt;setMaxResults( $limit ) -&gt;getQuery()-&gt;getResult(); </code></pre> <p>I need to add to that all the discussions that have current_category_id in their list of recommended_categories.</p> <p>So adding something like this:</p> <pre><code>$qb = self::$em-&gt;getRepository('Discussion')-&gt;createQueryBuilder('d'); $discussions = $qb-&gt;where('d.category='.$current_category_id) -&gt;orWhere($qb-&gt;expr()-&gt;in($current_category_id, 'd.recommended_categories')) -&gt;setFirstResult( $offset ) -&gt;setMaxResults( $limit ) -&gt;getQuery()-&gt;getResult(); </code></pre> <p>Which gives an SQL error because it doesn't like the "4 IN(d.recommended_categories))" in the SQL.</p> <p>This is what the recommended_categories looks like in the Discussions entity:</p> <pre><code>/** * @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; </code></pre> <p>And here is the Category entity:</p> <pre><code>/** @Id @Column(type="integer") @GeneratedValue **/ protected $id; /** @Column(type="string", unique=true, nullable=false,name="Name") **/ protected $name; </code></pre> <p>I also tried playing around with exists (does $current_category_id exist in d.recommended_categories) but nothing worked.</p> <p><strong>Any ideas how I can check if a given value (not a column) exists in a list of values associated with that entity?</strong></p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload