Skip to content
Advertisement

@DoctrineORMMapping “Annotation was never imported”

so i’m new to Doctrine and PHP in general and i have a small issue that i don’t know how to fix… I need to create a PHP app (using Doctrine) and to make communication with the DB my predecessor used Doctrine ORM; so i tried to use one of his files as a templates to make my part of the app but it does not seem to work…

First, his file used as template:

<?php

namespace AppEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use SymfonyComponentSerializerAnnotationGroups;

/**
 * @ORMEntity(repositoryClass="AppRepositoryUserRepository")
 * @UniqueEntity("email")
 */
class User implements UserInterface
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     * @Groups({"campaign_get", "user_logged"})
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=180, unique=true)
     * @AssertNotBlank
     * @AssertEmail
     * @Groups({"campaign_get", "user_logged"})
     */
    private $email;
    
    /**
     * @var string The hashed password
     * @ORMColumn(type="string")
     * @AssertNotBlank
     *  @AssertRegex(
     *  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;

    /**
     * @ORMColumn(type="string", length=100)
     * @AssertNotBlank
     * @Groups("campaign_get")
     */
    private $firstname;

    /**
     * @ORMColumn(type="string", length=100)
     * @AssertNotBlank
     * @Groups("campaign_get")
     */
    private $lastname;

    /**
     * @ORMColumn(type="boolean")
     */
    private $status;

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

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

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

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

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

    /**
     * @ORMColumn(type="datetime")
     */
    private $createdAt;

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

    /**
     * @ORMManyToMany(targetEntity="AppEntityRole")
     * @Groups("campaign_get")
     */
    private $userRoles;

    /**
     * @ORMManyToMany(targetEntity="AppEntityArticle", mappedBy="users")
     */
    private $articles;

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

    /**
     * @ORMManyToMany(targetEntity="AppEntityCampaign", mappedBy="users")
     */
    private $campaigns;

    /**
     * @ORMColumn(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;
    }
}

Then, mine using the same philosophy:

<?php

namespace AppEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use SymfonyComponentSerializerAnnotationGroups;

/**
 * @ORMEntity(repositoryClass="AppRepositoryAgencyRepository")
 */
class Agency
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMnameAgency()
     * @ORMColumn(type"string")
     */
    private $nameAgency;

    /**
     * @ORMnameContact
     * @ORMColumn(type="string")
     * @AssertNotBlank
     */
    private $nameContact;
}

Then i try to run this:

php bin/console doctrine:migrations:diff

into a terminal to update the DB, it works fine with the previous file (User Class) but mine (Agency Class) throw a error:

[Semantical Error] The annotation "@DoctrineORMMappingnameAgency" in property AppEntityAgency::$nameAgency was never imported. Did you maybe forget to add    
  a "use" statement for this annotation?  

At first i thought it was an issue with import, so i made exactly the sames imports as in User Class but the error is still present.

Googling the error lead me to a github issue that leeds to this as a fix; but it doesn’t seems to work’s for me…

What should i do to fix this issue?

Advertisement

Answer

You are using

@ORMnameAgency()

obviously this annotation does not exist, why do you have it there?

Remove @ORMnameAgency() and @ORMnameContact, it doesn’t make any sense.

Explanation: by @ORMnameAgency() you actually mean DoctrineORMMappingnameAgency and this annotation naturally doesn’t exist in Doctrine.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement