src/Repository/UserRepository.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\User;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\ORM\OptimisticLockException;
  6. use Doctrine\ORM\ORMException;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  9. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. /**
  12. * @extends ServiceEntityRepository<User>
  13. *
  14. * @method User|null find($id, $lockMode = null, $lockVersion = null)
  15. * @method User|null findOneBy(array $criteria, array $orderBy = null)
  16. * @method User[] findAll()
  17. * @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  18. */
  19. class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
  20. {
  21. public function __construct(ManagerRegistry $registry)
  22. {
  23. parent::__construct($registry, User::class);
  24. }
  25. /**
  26. * @throws ORMException
  27. * @throws OptimisticLockException
  28. */
  29. public function add(User $entity, bool $flush = true): void
  30. {
  31. $this->_em->persist($entity);
  32. if ($flush) {
  33. $this->_em->flush();
  34. }
  35. }
  36. /**
  37. * @throws ORMException
  38. * @throws OptimisticLockException
  39. */
  40. public function remove(User $entity, bool $flush = true): void
  41. {
  42. $this->_em->remove($entity);
  43. if ($flush) {
  44. $this->_em->flush();
  45. }
  46. }
  47. /**
  48. * Used to upgrade (rehash) the user's password automatically over time.
  49. */
  50. public function upgradePassword(UserInterface $user, string $newHashedPassword): void
  51. {
  52. if (!$user instanceof User) {
  53. throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
  54. }
  55. $user->setPassword($newHashedPassword);
  56. $this->_em->persist($user);
  57. $this->_em->flush();
  58. }
  59. // /**
  60. // * @return User[] Returns an array of User objects
  61. // */
  62. /*
  63. public function findByExampleField($value)
  64. {
  65. return $this->createQueryBuilder('u')
  66. ->andWhere('u.exampleField = :val')
  67. ->setParameter('val', $value)
  68. ->orderBy('u.id', 'ASC')
  69. ->setMaxResults(10)
  70. ->getQuery()
  71. ->getResult()
  72. ;
  73. }
  74. */
  75. /*
  76. public function findOneBySomeField($value): ?User
  77. {
  78. return $this->createQueryBuilder('u')
  79. ->andWhere('u.exampleField = :val')
  80. ->setParameter('val', $value)
  81. ->getQuery()
  82. ->getOneOrNullResult()
  83. ;
  84. }
  85. */
  86. }