I created my query builder, with a join and a condition, but the returned result on the joined data is full and is not filtered according to my join condition (group.id = 3).
How to modify my query or entities to get the affiliations and groups filtered accordingly?
Thank you.
Query in repository
$affiliationsGroup = 3;
$query = $this->createQueryBuilder('u');
$query->join('u.affiliations', 'a');
$query->join('a.group', 'g');
$query->andWhere('g.id = :group');
$query->setParameter('group', $affiliationsGroup);
$query->getQuery();
returned result
[
{
"id": 42,
"name":"user42";
"affiliations": [
{
"id": 1,
"group": {
"id": 1,
"name": "group1",
}
},
{
"id": 94,
"group": {
"id": 3,
"name": "group3"
},
}
]
User Entity
/**
* @ORM\OneToMany(targetEntity=Affiliations::class, mappedBy="user", cascade={"persist"})
* @Serializer\Groups({"student"})
*/
private $affiliations;
Affiliation Entity
/**
* @var \Users
*
* @ORM\ManyToOne(targetEntity="Users", inversedBy="affiliations", cascade={"persist"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
* @Serializer\Groups({"student"})
*/
private $user;
/**
* @var \Groups
*
* @ORM\ManyToOne(targetEntity="Groups", inversedBy="affiliations")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="group_id", referencedColumnName="id")
* })
* @Serializer\Groups({"public", "student"})
*/
private $group;
Controller User
/**
*
* Get all users
*
* @Rest\Get("/api/admin/users", name="get_users")
* @Rest\View(serializerGroups={"student"})
* @param Request $request
* @return View
*/
public function indexAction(Request $request): View
{
//Access repository and service to call function and my query builder
}