How to group Entity with another object with Symfony

I am working on a symfony project using doctrine

So, I have 3 objects:

  • Project
  • Activity
  • Category

The project has actions with several groups, and activity has several categories

I am trying to get all the actions grouped by project categories.

What is the right way to do this?

I tried with QueryBuilder, but it seems that you cannot group with another object, only with a value (tell me if I am wrong)

$q = $repo->createQueryBuilder('a')
        ->leftJoin('a.project', 'p')
        ->leftJoin('a.categories', 'category')
        ->where('p.id = ?1')
        ->setParameter(1, $projectId)
        ->groupBy('category.id')
        ->getQuery();
    return $q->getResult();

EDIT I really need to show a list of categories with corresponding actions inside

-Category1
     -Activity1
     -Activity2
     -Activity3
-Category2
     -Activity3
     -Activity4
-Category4
     -Activity1
     -Activity2
     -Activity3
     -Activity4
     -Activity5

My way of doing this is good, and my code is wrong? Or am I mistaken everywhere?

Thank you very much for your time!

+4
1

, :

$q = $categoryRepo->createQueryBuilder('c')
    ->leftJoin('c.actions', 'a')
    ->leftJoin('a.project', 'p')
    ->where('p.id = ?1')
    ->setParameter(1, $projectId)
    ->getQuery();
return $q->getResult();

, ( Twig?)

{% for category in categories %}
 - {{ category }}
    {% for action in category.actions if action.project.id = 1 %}
     - {{ action }}
    {% endfor %}
{% endfor %}
+1

Source: https://habr.com/ru/post/1584045/


All Articles