Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] how-to multiple entities #318

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php

namespace App\Entity;

use App\Repository\ResetPasswordRequestRepository;
use Doctrine\ORM\Mapping as ORM;
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestTrait;

#[ORM\Entity(repositoryClass: ResetPasswordRequestRepository::class)]
class ResetPasswordRequest implements ResetPasswordRequestInterface
{
use ResetPasswordRequestTrait;

#[ORM\Id, ORM\GeneratedValue, ORM\Column]
private ?int $id = null;

#[ORM\ManyToOne]
- #[ORM\JoinColumn(nullable: false)]
+ #[ORM\JoinColumn(nullable: true)]
private ?User $user = null;

+ #[ORM\ManyToOne]
+ #[ORM\JoinColumn(nullable: true)]
+ private ?Admin $admin = null;

public function __construct(
- User $user,
+ User|Admin $user,
\DateTimeInterface $expiresAt,
string $selector,
string $hashedToken,
) {
- $this->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.
Expand Down
Loading