Honza Prášil
Honza Prášil

Reputation: 70

Doctrine - Criteria - expressions - contains (many to many)

i am trying to solve a problem with Doctrine. I am using criteria to find results - posts (mostly using expression eq, that works), but now I would like to find posts by tags - in general ManyToMany (easily with criteria).

The problem is that i am still getting this error: "Notice: Undefined index: joinColumns in ..."

Here is my code:

....

$criteria = Criteria::create();

foreach ($this->tags as $tag)
    $criteria->Where( Criteria::expr()->contains('tags', $tag ));
...

//$this->tags is an array collection of selected tags.

Post entity - tags annotations:

/**
 * @var Collection
 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="posts")
 * @ORM\JoinTable(name="blog_post_tag",
 *                joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
 *                inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
 * )
 *
 */
protected $tags;

Is it possible to use criteria there?

Thank you.

Upvotes: 3

Views: 2976

Answers (1)

Richard
Richard

Reputation: 4129

Criteria with a manytomany relationship wasn't supported in doctrine a while ago, but I see this pull request:

https://github.com/doctrine/doctrine2/pull/885/commits

So it is presumably supported in more recent versions of doctrine.

Upvotes: 3

Related Questions