Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony] Doctrine nie rozpoznaje pola
Resurrection
post 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;
    }

  
}
Go to the top of the page
+Quote Post
rad11
post 21.11.2020, 19:09:10
Post #2





Grupa: Zarejestrowani
Postów: 1 270
Pomógł: 184
Dołączył: 7.10.2012
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Masz chyba źle dodana definicje Column dla email.

Ten post edytował rad11 21.11.2020, 19:09:31
Go to the top of the page
+Quote Post
Resurrection
post 21.11.2020, 22:11:43
Post #3





Grupa: Zarejestrowani
Postów: 82
Pomógł: 1
Dołączył: 23.02.2012

Ostrzeżenie: (0%)
-----


Co z nią jest nie tak?
Go to the top of the page
+Quote Post
rad11
post 22.11.2020, 09:04:42
Post #4





Grupa: Zarejestrowani
Postów: 1 270
Pomógł: 184
Dołączył: 7.10.2012
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Jak już to tak powinno byc:

  1. /**
  2.   * @ORM\Column(type="string", length=180, unique=true)
  3.   *
  4.   * @Assert\NotBlank
  5.   * @Assert\Email
  6.   * @AcmeAssert\IsEmailD
  7.   */
Go to the top of the page
+Quote Post
Resurrection
post 22.11.2020, 10:39:04
Post #5





Grupa: Zarejestrowani
Postów: 82
Pomógł: 1
Dołączył: 23.02.2012

Ostrzeżenie: (0%)
-----


Słuszna uwaga. Dziękuję za pomoc
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 29.03.2024 - 10:31