Symfony 5.1:无法识别的字段:activation_token

Posted

技术标签:

【中文标题】Symfony 5.1:无法识别的字段:activation_token【英文标题】:Symfony 5.1 : Unrecognized field: activation_token 【发布时间】:2021-02-26 17:25:21 【问题描述】:

我正在使用 Symfony 5.1 开发一个项目,我的一行代码在另一个项目中运行良好,但在当前项目中它给了我一个错误,即使我的代码是正确的。

我的 activation_token 列确实存在于表中,但它给了我错误。

$user = $this->getDoctrine()->getRepository(User::class)->findOneBy(['activation_token' => $token]);

在另一个函数中,我使用了下面的代码,它可以工作:

$user_exist = $this->getDoctrine()->getRepository(User::class)->findOneBy(['email' => $new_email]);

当两个属性都在我的数据库中时,第一个代码不起作用,第二个代码起作用。

我的用户实体:

<?php

namespace App\Entity;

use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @UniqueEntity(fields="email", message="There is already an account with this email")
 */
class User implements UserInterface

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     */
    private $email;

    /**
     * @ORM\Column(type="json")
     */
    private $roles = [];

    /**
     * @var string The hashed password
     * @ORM\Column(type="string")
     */
    private $password;

    /**
     * @ORM\Column(type="boolean")
     */
    private $isVerified = false;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $firstName;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $lastName;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $createdAt;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $updatedAt;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $activationToken;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $activationCode;

    /**
     * @ORM\OneToMany(targetEntity=Entreprise::class, mappedBy="user", orphanRemoval=true)
     */
    private $entreprises;

    public function __construct()
    
        $this->entreprises = new ArrayCollection();
    

    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;
    

    /**
     * @see UserInterface
     */
    public function getSalt()
    
        // not needed when using the "bcrypt" algorithm in security.yaml
    

    /**
     * @see UserInterface
     */
    public function eraseCredentials()
    
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    

    public function isVerified(): bool
    
        return $this->isVerified;
    

    public function setIsVerified(bool $isVerified): self
    
        $this->isVerified = $isVerified;

        return $this;
    

    public function getFirstName(): ?string
    
        return $this->firstName;
    

    public function setFirstName(string $firstName): self
    
        $this->firstName = $firstName;

        return $this;
    

    public function getLastName(): ?string
    
        return $this->lastName;
    

    public function setLastName(string $lastName): self
    
        $this->lastName = $lastName;

        return $this;
    

    public function getCreatedAt(): ?\DateTimeInterface
    
        return $this->createdAt;
    

    public function setCreatedAt(?\DateTimeInterface $createdAt): self
    
        $this->createdAt = $createdAt;

        return $this;
    

    public function getUpdatedAt(): ?\DateTimeInterface
    
        return $this->updatedAt;
    

    public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
    
        $this->updatedAt = $updatedAt;

        return $this;
    

    public function getActivationToken(): ?string
    
        return $this->activationToken;
    

    public function setActivationToken(?string $activationToken): self
    
        $this->activationToken = $activationToken;

        return $this;
    

    public function getActivationCode(): ?string
    
        return $this->activationCode;
    

    public function setActivationCode(?string $activationCode): self
    
        $this->activationCode = $activationCode;

        return $this;
    

    /**
     * @return Collection|Entreprise[]
     */
    public function getEntreprises(): Collection
    
        return $this->entreprises;
    

    public function addEntreprise(Entreprise $entreprise): self
    
        if (!$this->entreprises->contains($entreprise)) 
            $this->entreprises[] = $entreprise;
            $entreprise->setUser($this);
        

        return $this;
    

    public function removeEntreprise(Entreprise $entreprise): self
    
        if ($this->entreprises->removeElement($entreprise)) 
            // set the owning side to null (unless already changed)
            if ($entreprise->getUser() === $this) 
                $entreprise->setUser(null);
            
        

        return $this;
    

我的数据库捕获

enter image description here

【问题讨论】:

【参考方案1】:

你需要使用属性名而不是数据库列名,所以:

    $user = $this->getDoctrine()->getRepository(User::class)->findOneBy(['activationToken' => $token]);
    

【讨论】:

非常感谢,它工作得很好,但到目前为止我不明白为什么activation_token 在另一个项目中工作,但这个项目没有。 您在我的另一个项目中是对的,我的用户实体中确实有activation_token 属性。我刚刚检查过。

以上是关于Symfony 5.1:无法识别的字段:activation_token的主要内容,如果未能解决你的问题,请参考以下文章

mongoexport 错误:失败:解析失败 + 无法识别的字段“快照”

无法识别的字段类未标记为可忽略

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“消息”异常

引起:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段“状态”

Spark Confluent 模式注册表客户端 - 无法识别的字段“schemaType”