0

Could this be a bug in Sonata Admin bundle?

Let's say I have an entity A that can have multiple B's. To see this in my database, I would write something like

select * from a inner join b on b.a_id = a.id;

This would give me a result set with every A and the corresponding B (if any), for example:

| a.id | b.id | b.a_id |
| 1    | 1    | 1      |
| 2    | 2    | 2      |
| 2    | 3    | 2      |

In my admin class, I've overridden the createQuery function like this:

public function createQuery($context = 'list')
{
    $query = parent::createQuery($context);
    $rootAlias = current($query->getRootAliases());

    $query
        ->innerJoin('b', 'b', 'WITH', $rootAlias.'.b = b')
        ->addSelect('b');

    return $query;
}

The above gives me the desired SQL and the SQL itself returns the data as shown above, but Sonata Admin list view displays it like this:

| a.id | b.id | b.a_id |
| 1    |      |        |
|      | 1    | 1      |
| 2    |      |        |
|      | 2    | 2      |
|      | 3    | 2      |

So it seems I'm getting an extra row for each A-entity with no B-associations, followed by the B-entities with none of the values from the A-entity.

Why does that happen? Is it a bug? And how can I work around it?

henrik242
  • 366
  • 4
  • 11
  • So you are wanting `A` repeated for every `B`? It looks like you have an `AAdmin` and maybe you really want a `BAdmin`. – Arleigh Hix May 02 '21 at 21:34
  • Yes, A should be repeated for every matching B. I also have a B admin, and I tried to accomplish the same, but with the same result. My goal is to add an extra one-to-many join, so I will have A's with many B's and B's with many C's - all in one list. – henrik242 May 03 '21 at 05:07

0 Answers0