1

I have an entity Image which has the property "categories". Categories is an instance of Doctrine's ArrayCollection and holds different Category objects.

I now want to fetch all Image objects containing the Category object "main".

With normal properties this looks like this:

$repository->findBy(array('category' => 'main'));

Is this also possible with Array properties or do I have to fetch the Images over the Category side?

Regards!

1 Answer 1

1

The ->findBy() method only works on the owning side of relations.

You can also create a custom method in your repostiory:

public function findByCategoryName($categoryName)
{
    return $this
        ->createQueryBuilder('image')
        ->innerJoin('image.categories', 'category')
        ->where('category.name = :categoryName')
        ->setParameter('categoryName', $categoryName)
        ->getQuery()
        ->getResult()
    ;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, would you self stick to inversing or owning side? Or would you leave out a ManyToMany Category <-> Image relation at all?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.