Trying to shake some rust off, and understand how I'd do this with the query builder! I've tried a few approaches, and was met with all kinds of cryptic errors. Haven't visited this way of doing things for awhile, and appreciate the help!
I have a User entity, that belongs to Groups:
/**
* @ORM\Entity
* @ORM\Table(name="users")
*
class User {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer", nullable=false, options={"unsigned"=true})
*/
private $id;
/**
* @ORM\ManyToMany(targetEntity="Group", cascade={"persist"})
*/
private $groups;
}
/**
* @ORM\Entity
* @ORM\Table(name="`groups`")
*
*/
class Group {
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer", nullable=false, options={"unsigned"=true})
*/
private $id;
}
Now, I also have a separate GroupsCerts class, which is a list of certifications that are required by specific groups.
/**
* @ORM\Entity
* @ORM\Table(name="groups_certs")
* This entity contains meta data about group-certification relationship
*
*/
class GroupCertRecord
{
/**
* @var Group
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Group")
* @ORM\JoinColumn(onDelete="cascade")
*/
protected $group;
/**
* @var Certification
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Certification", inversedBy="group_records")
* @ORM\JoinColumn(onDelete="cascade")
*/
protected $cert;
/**
* @var \DateTime
* @ORM\Column(type="datetime", nullable=false, options={"default": "CURRENT_TIMESTAMP"})
*/
private $time_added;
}
The task is to get all users, that belong to groups, to which a specific certification is indexed.
Simple SQL
I'm currently using the ResultSetMappingBuilder very nicely, the gist of the SQL using that approach is very simple. E.g., looking for certificate 3, the SQL is:
SELECT U.*
FROM users U
WHERE U.id IN (
SELECT user_id
FROM users_groups UG
WHERE UG.group_id IN (
SELECT id
FROM groups G
WHERE G.id IN (
SELECT group_id
FROM groups_certs GC
WHERE GC.cert_id=3
)
)
)
I had tried to map this into a QueryBuilder expression, but it's dying at the expression builder for "IN". It doesn't like something I am doing - quick solve?
My Failed Attempt
$em = $this->getEntityManager();
$expr = $em->getExpressionBuilder();
$subQuery = $em->createQueryBuilder()
->select('(gc.group)')
->from(GroupCertRecord::class, 'gc')
->where('gc.cert = :cert');
$queryD = $em->createQueryBuilder()
->select('u')
->from(User::class, 'u')
->where($expr->in('u.groups', $subQuery->getDQL()))
->setParameter('cert', $cert);
Thanks!