I have 3 tables in doctrine, for simplicity's sake I'll call them: U
, M
and R
U
can have M
's and R
's
M
can have R
's
R
can have nothing
As a diagram this might look like:
U ---> M
| |
| V
'----> R
what I want to do is create one query to get all R
's related to U
either through M
or directly ordered by a column in R
and queried by a column in R
.
in PHP currently I have:
$qb->select('r')
->from('R','r')
->where('r.u = :uid')
->orderBy('r.order','DESC')
->setParameter('uid', $U->getId())
->setFirstResult( $offset )
->setMaxResults( $limit )
->andWhere(
$qb->expr()->orX(
$qb->expr()->like('r.q1',':query'),
$qb->expr()->like('r.q2',':query')
)
)
->setParameter('query', "%" . $query . "%");
I want to join m.r
's somehow while still being able to query their properties. I have tried:
$qb->select('u.r, m.r')
->from('U','u')
->where('u.id = :uid')
->join('u.m','m')
->orderBy('u.r.order','DESC')
->setParameter('uid', $U->getId())
->setFirstResult( $offset )
->setMaxResults( $limit )
->andWhere(
$qb->expr()->orX(
$qb->expr()->like('u.r.q1',':query'),
$qb->expr()->like('u.r.q2',':query')
)
)
->setParameter('query', "%" . $query . "%");
As well as many other things but to no avail...
thanks in advance!
Okay so what I ended up doing was:
Select all the ID's from M
where u.id = :uid
then, select all from R
where r.u = :uid OR r.m IN (result from first query)
Simple really, I don't know why I didn't think of it earlier...