Home > front end >  QueryBuilder a ManyToMany to find all not in the jointable in Doctrine
QueryBuilder a ManyToMany to find all not in the jointable in Doctrine

Time:05-09

I have a ManyToMany relation in Doctrine, I created entities for table A and table B and Doctrine created a junction table C.

I'm trying to bring all results from table B that have no realtionship in table C.

Have tried many approaches but no success, about to remake this as a ManyToOne as last restor...

So far have tried using "MEMBER OF", native SQL, DQL, no success, can someone teach me how it works when querying when tables have attributes collection and not columns?

$query = $qb->select('a')
                ->innerJoin('a.clusters', 'c', Join::WITH, 'a.id = c.id')
                ->getQuery();

Return zero results :/

CodePudding user response:

you can use this:

$query = $qb->select('b')
        ->leftJoin('b.c', 'c')
        ->where('c.id != b.id')
         ->getQuery();

CodePudding user response:

Thanks to Sadegh Rohani insight, I was able to find the answer

$qb->select('a')
   ->leftJoin('a.c', 'c')
   ->where('c.id is null');

This querybuilder will find all entities a that do not belong to any c, which in manytomany relation is an arrayCollection attribute inside 'a' entity.

  • Related