find($id); } /** * Overwritten to fetch preferences when using the Profile controller actions. * Depends on the query, some magic mechanisms like the ParamConverter will use this method to fetch the user. */ public function findOneBy(array $criteria, array $orderBy = null) { if (count($criteria) == 1 && isset($criteria['username'])) { return $this->loadUserByUsername($criteria['username']); } return parent::findOneBy($criteria, $orderBy); } /** * @return int */ public function countUser() { return $this->count([]); } /** * @param UserQuery $query * @return array|\Doctrine\ORM\QueryBuilder|\Pagerfanta\Pagerfanta */ public function findByQuery(UserQuery $query) { $qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('u') ->from(User::class, 'u') ->orderBy('u.' . $query->getOrderBy(), $query->getOrder()); if (UserQuery::SHOW_VISIBLE == $query->getVisibility()) { $qb->andWhere('u.enabled = 1'); } elseif (UserQuery::SHOW_HIDDEN == $query->getVisibility()) { $qb->andWhere('u.enabled = 0'); } if ($query->getRole() !== null) { $rolesWhere = 'u.roles LIKE :role'; $qb->setParameter('role', '%' . $query->getRole() . '%'); // a hack as FOSUserBundle does not save the ROLE_USER in the database as it is the default role if ($query->getRole() === User::ROLE_USER) { $rolesWhere .= ' OR u.roles LIKE :role1'; $qb->setParameter('role1', '%{}'); } $qb->andWhere($rolesWhere); } return $this->getBaseQueryResult($qb, $query); } /** * @param string $username * @return mixed|null|\Symfony\Component\Security\Core\User\UserInterface * @throws \Doctrine\ORM\NoResultException * @throws \Doctrine\ORM\NonUniqueResultException */ public function loadUserByUsername($username) { return $this->createQueryBuilder('u') ->select('u', 'p') ->leftJoin('u.preferences', 'p') ->where('u.username = :username') ->orWhere('u.email = :username') ->setParameter('username', $username) ->getQuery() ->getSingleResult(); } }