[Symfony] Doctrine nie rozpoznaje pola |
[Symfony] Doctrine nie rozpoznaje pola |
21.11.2020, 19:05:49
Post
#1
|
|
Grupa: Zarejestrowani Postów: 82 Pomógł: 1 Dołączył: 23.02.2012 Ostrzeżenie: (0%) |
Witam,
W skrócie. Dodaje własne sprawdzenie walidacji pola formularza. Korzystam z Form i walidacji Symfony poprzez adnotację. W funkcji walidacji potrzebuję dostęp do bazy danych. W tym celu do konstruktora klasy dziedziczącej po ConstraintValidator podaje obiekt klasy EntityManagerInterface i przetrzymuję w zmiennej $entityMan. Następnie w funkcji validate tej klasy staram się wyszukać za pomocą findOneBy() obiekt(wiersz w bazie danych) w którym wartość email równa się wartości $value przekazanej do funkcji validate. Jednak otrzymuje błąd ORMException "Unrecognized field: email". Dodam, że w klasie User oraz w bazie danych takie pole istnieje i ma tą samą nazwę. Proszę o wskazówkę gdzie może być błąd. Kod <?php namespace App\Validator\Constraints; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; use Symfony\Component\Validator\Exception\UnexpectedValueException; use App\Entity\User; use App\Repository\UserRepository; use Doctrine\ORM\EntityManagerInterface; class IsEmailDValidator extends ConstraintValidator { private $entityMan; public function __construct(EntityManagerInterface $entityManagerInterface) { $this->entityMan=$entityManagerInterface; } public function validate($value, Constraint $constraint) { if (!$constraint instanceof IsEmailD) { throw new UnexpectedTypeException($constraint, IsEmailD::class); } if (null === $value || '' === $value) { return; } if (!is_string($value)) { throw new UnexpectedValueException($value, 'string'); } $user = $this->entityMan->getRepository(User::class) ->findOneBy(array('email' => $value)); if(!$user) { $this->context->buildViolation($constraint->message)->addViolation(); } } } Kod <?php
namespace App\Entity; use App\Repository\UserRepository; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Validator\Constraints as Assert; use App\Validator\Constraints as AcmeAssert; /** * @ORM\Entity(repositoryClass=UserRepository::class) */ class User implements UserInterface { /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", length=180, unique=true) */ /** * @Assert\NotBlank * @Assert\Email * @AcmeAssert\IsEmailD */ private $email; /** * @ORM\Column(type="json") */ private $roles = []; /** * @var string The hashed password * @ORM\Column(type="string") */ private $password; public function getId(): ?int { return $this->id; } public function getEmail(): ?string { return $this->email; } public function setEmail(string $email): self { $this->email = $email; return $this; } /** * A visual identifier that represents this user. * * @see UserInterface */ public function getUsername(): string { return (string) $this->email; } /** * @see UserInterface */ public function getRoles(): array { $roles = $this->roles; // guarantee every user at least has ROLE_USER $roles[] = 'ROLE_USER'; return array_unique($roles); } public function setRoles(array $roles): self { $this->roles = $roles; return $this; } /** * @see UserInterface */ public function getPassword(): string { return (string) $this->password; } public function setPassword(string $password): self { $this->password = $password; return $this; } } |
|
|
22.11.2020, 09:04:42
Post
#2
|
|
Grupa: Zarejestrowani Postów: 1 270 Pomógł: 184 Dołączył: 7.10.2012 Skąd: Warszawa Ostrzeżenie: (0%) |
|
|
|
Wersja Lo-Fi | Aktualny czas: 21.09.2024 - 22:27 |