Need to create a query for two Doctrine entities with ManyToMany relations

I have 2 tables, Models and User, with manytomany relations. I need to create a query that produce all $models for a $user that matches a specific title string.

I was able to get this query to work but it only produces an array of the titles field.

/**
 * Returns distinct array of users models titles
 *
 * @param User $user
 *
 * @return array
 */
public function getModelTitlesForUser(User $user)
{
    $qb = $this->createQueryBuilder('m')
        ->select('distinct(m.title) as title')
        ->innerJoin('m.users', 'u')
        ->where('u.id = :user_id')
        ->setParameter('user_id', $user->getId());
    $this->useResultCacheOnQuery($qb);

    return $qb->getQuery()->getResult();
}

But I need an array of Models objects instead.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *