phpsymfonysymfony5bolt-cms

List of users in Symfony 5 (Bolt 4)


I'm using Bolt 4 CMS which is based on Symfony 5. In a controller I wrote, I would like to list all the users from my database, to retrieve their email addresses and send them an email. For now I am simply trying to retrieve the email address from the username.

In this example https://symfony.com/doc/current/security/user_provider.html, it shoes how to create your own class to deal with users from the database:

// src/Repository/UserRepository.php
namespace App\Repository;

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;

class UserRepository extends ServiceEntityRepository implements UserLoaderInterface
{
    // ...

    public function loadUserByUsername(string $usernameOrEmail)
    {
        $entityManager = $this->getEntityManager();

        return $entityManager->createQuery(
                'SELECT u
                FROM App\Entity\User u
                WHERE u.username = :query
                OR u.email = :query'
            )
            ->setParameter('query', $usernameOrEmail)
            ->getOneOrNullResult();
    }
}

In my custom controller, I then call this class and function:

// src/Controller/LalalanEventController.php
namespace App\Controller;

use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;

use App\Repository\LalalanUserManager;

class LalalanEventController extends AbstractController
{
    /**
     * @Route("/new_event_email")
     */
    private function sendEmail(MailerInterface $mailer)
    {
        $userManager = new LalalanUserManager();
        
        $email = (new Email())
            ->from('aaa.bbb@ccc.com')
            ->to($userManager('nullname')->email)
            ->subject('Nice title')
            ->text('Sending emails is fun again!')
            ->html('<p>See Twig integration for better HTML integration!</p>');

        $mailer->send($email);
    }
}

Unfortunately, in the example, the class extends from ServiceEntityRepository, which requires a ManagerRegistry for the constructor. Does anyone have a clue what could I change to solve this?

Thanks in advance!


Solution

  • As said in the doc,

    User providers are PHP classes related to Symfony Security that have two jobs:

    • Reload the User from the Session
    • Load the User for some Feature

    So if you want only to get list of users, you have just to get the UserRepository like this:

        /**
         * @Route("/new_event_email")
         */
        private function sendEmail(MailerInterface $mailer)
        {
            $userRepository = $this->getDoctrine()->getRepository(User::class);
    
            $users = $userRepository->findAll();
    
            // Here you loop over the users
            foreach($users as $user) {
                /// Send email
            }
        }
    

    Doctrine reference: https://symfony.com/doc/current/doctrine.html

    You need also to learn more about dependency injection here: https://symfony.com/doc/current/components/dependency_injection.html