Home > Back-end >  Hello, I have a task. It is necessary to show all users on transition to a route
Hello, I have a task. It is necessary to show all users on transition to a route

Time:08-02

I have essence

Code

 <?php

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;


#[ORM\Entity(repositoryClass: UserRepository::class)]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(type: 'string', length: 255, nullable: false)]
    private string $name;

    #[ORM\Column(type: 'string', length: 255, nullable: false)]
    private string $surname;

    #[ORM\Column(type: 'string', length: 255, nullable: false)]
    private string $password;

    #[ORM\Column(type: 'string', length: 255, unique: true),
    email
    ]
    private string $email;

    #[ORM\Column(type: "json", nullable: false)]
    private array $roles = [];

    #[ORM\Column(name: 'authorization_count', type: 'integer', nullable: false)]
    private int $authorizationCount = 0;

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName(string $name): void
    {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getSurname(): string
    {
        return $this->surname;
    }

    /**
     * @param string $surname
     */
    public function setSurname(string $surname): void
    {
        $this->surname = $surname;
    }

    /**
     * @return string
     */
    public function getPassword(): string
    {
        return $this->password;
    }

    /**
     * @param string $password
     */
    public function setPassword(string $password): void
    {
        $this->password = $password;
    }

    /**
     * @return string
     */
    public function getEmail(): string
    {
        return $this->email;
    }

    /**
     * @param string $email
     */
    public function setEmail(string $email): void
    {
        $this->email = $email;
    }

I have a database with data

Data

Please tell me how can I show all users (name, surname, Email) by going along the route. I have a manager, a repository and a controller. Thanks in advance.

I tried to get the first name, last name, and email through the models, but I didn’t get it. I also tried to create a DTOBuilder and work through the manager, but it looks like I’m doing something wrong.

CodePudding user response:

if i understand you correct you just want to load all users from database in a controller method? you can do this like:

use App\Entity\User;

....

$userRepository = $this->getDoctrine()->getRepository(User::class);
$users = $userRepository->findAll();
  • Related