Reputation: 343
I have a Profile and User Entity. Profiles can be related to users in one of three ways:
It's a basic permissions structure. What I need to do is get all Profiles by User, regardless of permissions granted. I think if I were using Doctrine 2.5.* I'd be ok as QueryBuilder has the 'member of' comparison added, but I'm on Symfony 2.7.* and unfortunately Doctrine only goes up to 2.4.* (some kind of stability issue it seems if you make the jump to Doctrine 2.5 with less than Symfony 3).
I'm unsure how to replicate the functionality though.
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityRepository;
class ProfileRepository extends EntityRepository
{
/**
* @param User $user
*
* @return ArrayCollection
*/
public function getAllByUser(User $user, $status = NULL)
{
$qb = $this->createQueryBuilder('p');
$e = $qb->expr();
$qb->where($e->eq('p.primary_user', ':user'));
// Some kind of comparison? a join?;
$qb->setParameter('user', $user);
$qb->setParameter('userId', $user->getId());
if ($status) {
$qb->andWhere($e->eq('p.status', ':status'));
$qb->setParameter('status', $status);
}
return $qb->getQuery()->getResult();
}
}
Upvotes: 1
Views: 1471
Reputation: 1244
Solutions...
Using DQL:
SELECT p
FROM Profile p
WHERE p.primaryUser = :user
OR :user MEMBER OF p.editUsers
OR :user MEMBER OF p.viewUsers
Using QueryBuilder:
$qb = $this->createQueryBuilder("p");
$qb
->where("p.primaryUser = :user")
->orWhere(":user MEMBER OF p.editUsers")
->orWhere(":user MEMBER OF p.viewUsers")
->setParameter("user", $user)
;
Upvotes: 6