@Doctrine\ORM\Mapping\"Annotation was never imported"

@Doctrine\ORM\Mapping\ "Annotation was never imported"

所以我是 Doctrine 的新手,一般来说 PHP 我有一个小问题不知道如何解决... 我需要创建一个 PHP 应用程序(使用 Doctrine)并与我的前任使用 Doctrine ORM 的数据库进行通信;所以我尝试使用他的一个文件作为模板来制作我的应用程序部分,但它似乎不起作用...

首先,他的文件用作模板:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @UniqueEntity("email")
 */
class User implements UserInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"campaign_get", "user_logged"})
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     * @Assert\NotBlank
     * @Assert\Email
     * @Groups({"campaign_get", "user_logged"})
     */
    private $email;
    
    /**
     * @var string The hashed password
     * @ORM\Column(type="string")
     * @Assert\NotBlank
     *  @Assert\Regex(
     *  pattern="/^(?=.{8,}$)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).*$/",
     *  message="Votre mot de passe doit contenir au moins 1 chiffre, 1 majuscule, 1 minuscule et avoir une longueur d'au moins 8 caractères."
     * )
     */
    private $password;

    /**
     * @ORM\Column(type="string", length=100)
     * @Assert\NotBlank
     * @Groups("campaign_get")
     */
    private $firstname;

    /**
     * @ORM\Column(type="string", length=100)
     * @Assert\NotBlank
     * @Groups("campaign_get")
     */
    private $lastname;

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

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

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

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

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

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

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

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

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Role")
     * @Groups("campaign_get")
     */
    private $userRoles;

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Article", mappedBy="users")
     */
    private $articles;

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

    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Campaign", mappedBy="users")
     */
    private $campaigns;

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

    public function __construct()
    {
        $this->userRoles = new ArrayCollection();
        $this->articles = new ArrayCollection();
        $this->status = 1;
        $this->createdAt = new \DateTime();
        $this->campaigns = 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->userRoles;

        $userRoles = [];

        foreach ($roles as $role) {
            $userRoles[] = $role->getName();
        }

        return $userRoles;
    }

    /**
     * @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 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 getStatus(): ?bool
    {
        return $this->status;
    }

    public function setStatus(bool $status): self
    {
        $this->status = $status;

        return $this;
    }

    public function getIdInstagram(): ?string
    {
        return $this->idInstagram;
    }

    public function setIdInstagram(?string $idInstagram): self
    {
        $this->idInstagram = $idInstagram;

        return $this;
    }

    public function getIdFacebook(): ?string
    {
        return $this->idFacebook;
    }

    public function setIdFacebook(?string $idFacebook): self
    {
        $this->idFacebook = $idFacebook;

        return $this;
    }

    public function getIdYoutube(): ?string
    {
        return $this->idYoutube;
    }

    public function setIdYoutube(?string $idYoutube): self
    {
        $this->idYoutube = $idYoutube;

        return $this;
    }

    public function getIdSnapchat(): ?string
    {
        return $this->idSnapchat;
    }

    public function setIdSnapchat(?string $idSnapchat): self
    {
        $this->idSnapchat = $idSnapchat;

        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 getIdTiktok(): ?string
    {
        return $this->idTiktok;
    }

    public function setIdTiktok(?string $idTiktok): self
    {
        $this->idTiktok = $idTiktok;

        return $this;
    }

    /**
     * @return Collection|Role[]
     */
    public function getUserRoles(): Collection
    {
        return $this->userRoles;
    }

    public function addUserRole(Role $userRole): self
    {
        if (!$this->userRoles->contains($userRole)) {
            $this->userRoles[] = $userRole;
        }

        return $this;
    }

    public function removeUserRole(Role $userRole): self
    {
        if ($this->userRoles->contains($userRole)) {
            $this->userRoles->removeElement($userRole);
        }

        return $this;
    }

    /**
     * @return Collection|Article[]
     */
    public function getArticles(): Collection
    {
        return $this->articles;
    }

    public function addArticle(Article $article): self
    {
        if (!$this->articles->contains($article)) {
            $this->articles[] = $article;
            $article->addUser($this);
        }

        return $this;
    }

    public function removeArticle(Article $article): self
    {
        if ($this->articles->contains($article)) {
            $this->articles->removeElement($article);
            $article->removeUser($this);
        }

        return $this;
    }

    public function getCompanyName(): ?string
    {
        return $this->companyName;
    }

    public function setCompanyName(?string $companyName): self
    {
        $this->companyName = $companyName;

        return $this;
    }

    /**
     * @return Collection|Campaign[]
     */
    public function getCampaigns(): Collection
    {
        return $this->campaigns;
    }

    public function addCampaign(Campaign $campaign): self
    {
        if (!$this->campaigns->contains($campaign)) {
            $this->campaigns[] = $campaign;
            $campaign->addUser($this);
        }

        return $this;
    }

    public function removeCampaign(Campaign $campaign): self
    {
        if ($this->campaigns->contains($campaign)) {
            $this->campaigns->removeElement($campaign);
            $campaign->removeUser($this);
        }

        return $this;
    }

    public function getResetToken(): ?string
    {
        return $this->resetToken;
    }

    public function setResetToken(?string $resetToken): self
    {
        $this->resetToken = $resetToken;

        return $this;
    }
}

然后,我使用相同的理念:

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ORM\Entity(repositoryClass="App\Repository\AgencyRepository")
 */
class Agency
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\nameAgency()
     * @ORM\Column(type"string")
     */
    private $nameAgency;

    /**
     * @ORM\nameContact
     * @ORM\Column(type="string")
     * @Assert\NotBlank
     */
    private $nameContact;
}

然后我尝试运行这个:

php bin/console doctrine:migrations:diff

进入终端更新数据库,它与之前的文件(用户 Class)一起工作正常,但我的(机构 Class)抛出错误:

[Semantical Error] The annotation "@Doctrine\ORM\Mapping\nameAgency" in property App\Entity\Agency::$nameAgency was never imported. Did you maybe forget to add    
  a "use" statement for this annotation?  

起初我以为这是导入的问题,所以我做了与用户完全相同的导入 Class 但错误仍然存​​在。

谷歌搜索错误导致我遇到 github 问题,该问题导致 this 作为修复;但它似乎对我不起作用...

我应该怎么做才能解决这个问题?

您正在使用

@ORM\nameAgency()

显然这个注解不存在,你为什么会有它?

去掉@ORM\nameAgency()@ORM\nameContact,没有任何意义。

解释:@ORM\nameAgency() 你实际上是指 Doctrine\ORM\Mapping\nameAgency 而这个注解在 Doctrine 中自然不存在。