From b5d57c308f11c4246e0800117d97d628a77e439d Mon Sep 17 00:00:00 2001 From: Jesse Rushlow Date: Sun, 28 Apr 2024 04:00:42 -0400 Subject: [PATCH] [docs] how-to multiple entities --- README.md | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/README.md b/README.md index 540c2922..8364159a 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,88 @@ public function profile(Request $request, User $user, ResetPasswordRequestReposi } ``` +### Multiple "User" Entities + +```diff +// ResetPasswordController + +private function processSendingPasswordResetEmail(string $emailFormData, MailerInterface $mailer, TranslatorInterface $translator): RedirectResponse + { + $user = $this->entityManager->getRepository(User::class)->findOneBy([ + 'email' => $emailFormData, + ]); + ++ if (null === $user) { ++ $user = $this->entityManager->getRepository(Admin::class)->findOneBy([ ++ 'email' => $emailFormData, ++ ]); + } + + // Do not reveal whether a user account was found or not. + if (!$user) { + return $this->redirectToRoute('app_check_email'); + } +... +``` + +```diff +user = $user; ++ if ($user instanceof User) { ++ $this->user = $user; ++ } else { ++ $this->admin = $user; ++ } + + $this->initialize($expiresAt, $selector, $hashedToken); + } + + public function getId(): ?int + { + return $this->id; + } + +- public function getUser(): User ++ public function getUser(): User|Admin + { +- return $this->user; ++ return $this->user ?? $this->admin; + } +} +``` + ## Support Feel free to open an issue for questions, problems, or suggestions with our bundle.