I am trying to implement KnpLabs / DoctrineBehaviors / Translatable in Symfony 5.2.
I have implemented two entities for translation as described in the docs, however, Symfony throws the following error:
Error: Class App\Entity\BlogPost contains 11 abstract methods and must therefore be declared abstract or implement the remaining methods (Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface::getTranslations, Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface::getNewTranslations, Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface::addTranslation, ...)
When I declare that class "abstract", I am getting the following error:
Trying to invoke abstract method Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface::getTranslationEntityClass()
How can I solve this problem?
Entity BlogPost
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;
/**
* #ORM\Entity
*/
abstract class BlogPost implements TranslatableInterface
{
use TranslationTrait;
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="integer", nullable=true)
*/
private $active;
public function getId(): ?int
{
return $this->id;
}
public function getActive(): ?int
{
return $this->id;
}
public function setActive(int $active): self
{
$this->active = $active;
return $this;
}
}
Entity BlogPostTranslation
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslationTrait;
/**
* #ORM\Entity
*/
class BlogPostTranslation implements TranslationInterface
{
use TranslationTrait;
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=100)
*/
protected $name;
/**
* #ORM\Column(type="string", length=255)
*/
protected $title;
/**
* #ORM\Column(type="string", length=100)
*/
protected $slug;
/**
* #ORM\Column(type="text", nullable=true)
*/
protected $text;
public function getId(): ?int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getTitle(): string
{
return $this->title;
}
public function setTitle(string $title): void
{
$this->title = $title;
}
public function getSlug(): string
{
return $this->slug;
}
public function setSlug(string $slug): void
{
$this->slug = $slug;
}
public function getText(): string
{
return $this->text;
}
public function setText(string $text): void
{
$this->text = $text;
}
}
Be looking at the bundle docs, you should use the TranslatableTrait and not the TranslationTrait for your BlogPost entity.
See the sample here: https://github.com/KnpLabs/DoctrineBehaviors/blob/master/docs/translatable.md
Related
I have a little problem with Doctrine and Symfony 5.4. I've searched on StackOverflow and on the doc, but every solution seems that is not correctly for me.
I have 3 class: Lang, State, OrderState. Last one is join with Lang and State through id_state and id_lang with a ManyToMany association relation ship.
When i execute the schema validate, i get the classic error. Honestly i don't understand why.
Mapping
-------
[FAIL] The entity-class App\Entity\OrderState mapping is invalid:
* The referenced column name 'id_state' has to be a primary key column on the target entity class 'App\Entity\OrderState'.
* The referenced column name 'id' has to be a primary key column on the target entity class 'App\Entity\Lang'.
Database
--------
In MissingColumnException.php line 15:
Column name "id_state" referenced for relation from App\Entity\OrderState towa
rds App\Entity\State does not exist.
State.php
<?php
namespace App\Entity;
use App\Repository\StateRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=StateRepository::class)
*/
class State
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id_state;
/**
* #ORM\Column(type="string", length=255)
*/
private $label;
public function getIdState(): ?int
{
return $this->id_state;
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
}
Lang.php
<?php
namespace App\Entity;
use App\Repository\LangRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=LangRepository::class)
*/
class Lang
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id_lang;
/**
* #ORM\Column(type="string", length=255)
*/
private $lang;
public function getIdLang(): ?int
{
return $this->id_lang;
}
public function setIdLang(int $id_lang): self
{
$this->id_lang = $id_lang;
return $this;
}
public function getLang(): ?string
{
return $this->lang;
}
public function setLang(string $lang): self
{
$this->lang = $lang;
return $this;
}
}
and OrderState.php
<?php
namespace App\Entity;
use App\Repository\OrderStateRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=OrderStateRepository::class)
*/
class OrderState
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToMany(targetEntity=State::class)
* #ORM\JoinColumn(name="id_state", referencedColumnName="id_state")
*/
private $id_state;
/**
* #ORM\ManyToMany(targetEntity=Lang::class)
* #ORM\JoinColumn(name="id_lang", referencedColumnName="id_lang")
*/
private $id_lang;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $state;
public function __construct()
{
$this->id_state = new ArrayCollection();
$this->id_lang = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* #return Collection<int, State>
*/
public function getIdState(): Collection
{
return $this->id_state;
}
public function addIdState(State $idState): self
{
if (!$this->id_state->contains($idState)) {
$this->id_state[] = $idState;
}
return $this;
}
public function removeIdState(State $idState): self
{
$this->id_state->removeElement($idState);
return $this;
}
/**
* #return Collection<int, Lang>
*/
public function getIdLang(): Collection
{
return $this->id_lang;
}
public function addIdLang(Lang $idLang): self
{
if (!$this->id_lang->contains($idLang)) {
$this->id_lang[] = $idLang;
}
return $this;
}
public function removeIdLang(Lang $idLang): self
{
$this->id_lang->removeElement($idLang);
return $this;
}
public function getState(): ?string
{
return $this->state;
}
public function setState(?string $state): self
{
$this->state = $state;
return $this;
}
}
in my symfony project, after adding the topo property to my Media entity related OneToMany to Topo entity I did the migrations,
php bin / console make: migration
It works .But with
php bin/console doctrine:migrations:migrate
I have the errors described on the two images.
In addition to that, the topo view route, topo_show, the following bug:
App \ Entity \ Topo object not found by the #ParamConverter annotation.
My entity file media is :
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\MediaRepository;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation\Uploadable;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass=MediaRepository::class)
* #Vich\Uploadable
*/
class Media
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity=Site::class, inversedBy="media")
*/
private $site;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $nom;
/**
* #ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $image;
/**
* #Vich\UploadableField(mapping="sites", fileNameProperty="image" )
*
*/
private $imageFile;
/**
* #ORM\Column(type="datetime", nullable=true)
*/
private $maj;
/**
* #ORM\ManyToOne(targetEntity=Topo::class, inversedBy="media")
* #ORM\JoinColumn(nullable=false)
*/
private $topo;
public function __toString(){
return $this->nom;
}
public function getId(): ?int
{
return $this->id;
}
public function getSite(): ?Site
{
return $this->site;
}
public function setSite(?Site $site): self
{
$this->site = $site;
return $this;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(?string $nom): self
{
$this->nom = $nom;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
/**
* Get the value of imageFile
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* Set the value of imageFile
*
* #return self
*/
public function setImageFile($imageFile)
{
$this->imageFile = $imageFile;
return $this;
}
/**
* Get the value of maj
*/
public function getMaj()
{
return $this->maj;
}
/**
* Set the value of maj
*
* #return self
*/
public function setMaj($maj)
{
$this->maj = $maj;
return $this;
}
public function getTopo(): ?Topo
{
return $this->topo;
}
public function setTopo(?Topo $topo): self
{
$this->topo = $topo;
return $this;
}
}
I can't find the solution yet.
And more, I have my API which crashed, which can no longer find the hydramember.
Could you help me? Thank you
I get
Expected argument of type "App\Entity\ObjectxDescription", "string"
given at property path "description".
When I try to save data from TextEditorField, I get the error above. The field displays the content correctly and is defined in the Crud Controller. I create CRUD Controller with form to edit Name and Description. Name variable is in Objectx Entity and Description in ObjectxDescription Entity.
crud controller:
<?php
//src/Controller/Admin/ObjectxCrudController.php
namespace App\Controller\Admin;
use App\Entity\Objectx;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use App\Entity\ObjectxDescription;
class ObjectxCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Objectx::class;
}
public function configureCrud(Crud $crud): Crud
{
return $crud
->setEntityLabelInSingular('Object')
->setEntityLabelInPlural('Objects')
->setPageTitle('index', 'Objects')
;
}
public function configureFields(string $pageName): iterable
{
return [
TextField::new('name','Name'),
TextEditorField::new('description','Description')
];
}
}
Objectx entity:
<?php
//src/Entity/Objectx.php
namespace App\Entity;
use App\Repository\ObjectxRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=ObjectxRepository::class)
*/
class Objectx
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\OneToOne(targetEntity=ObjectxDescription::class, cascade={"persist", "remove"})
* #ORM\JoinColumn(nullable=false)
*/
private $description;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?ObjectxDescription
{
return $this->description;
}
public function setDescription(ObjectxDescription $description): self
{
$this->description = $description;
return $this;
}
}
ObjectxDescription entity:
<?php
// src/Entity/ObjectxDescription.php
namespace App\Entity;
use App\Repository\ObjectxDescriptionRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=ObjectxDescriptionRepository::class)
*/
class ObjectxDescription
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="text", nullable=true)
*/
private $description;
public function __toString()
{
return $this->description;
}
public function getId(): ?int
{
return $this->id;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
}
i want to develop a doctrine-powered database application which should portray the following schema.
There are users, companies and relation-roles which describes the relation between a user to a company like [USER X] is [ROLE X] in [COMPANY X].
I'm using the symfony maker-bundle to create the entities I need. I'll attach every code at the end of this post.
To test the code, I persisted a company, a user, a role and a relation between them to the database. I expected that I could get all related users with their roles using a Company-Entity-Object with the generated getter getRelatedUsers() but I get an empty ArrayCollection.
This is how I tested to fetch the data in a TestController
#[Route('/test', name: 'test_page')]
public function index(CompanyRepository $companyRepository): Response
{
$companies = $companyRepository->findAll();
dd($companies[0]->getRelatedUsers());
}
Thanks for your help!
User.php
<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=UserRepository::class)
* #ORM\Table(name="`user`")
*/
class User
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $fullName;
/**
* #ORM\OneToMany(targetEntity=UserCompanyRelation::class, mappedBy="user")
*/
private $relatedCompanies;
public function __construct()
{
$this->relatedCompanies = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFullName(): ?string
{
return $this->fullName;
}
public function setFullName(string $fullName): self
{
$this->fullName = $fullName;
return $this;
}
/**
* #return Collection|UserCompanyRelation[]
*/
public function getRelatedCompanies(): Collection
{
return $this->relatedCompanies;
}
public function addRelatedCompany(UserCompanyRelation $relatedCompany): self
{
if (!$this->relatedCompanies->contains($relatedCompany)) {
$this->relatedCompanies[] = $relatedCompany;
$relatedCompany->setUser($this);
}
return $this;
}
public function removeRelatedCompany(UserCompanyRelation $relatedCompany): self
{
if ($this->relatedCompanies->removeElement($relatedCompany)) {
// set the owning side to null (unless already changed)
if ($relatedCompany->getUser() === $this) {
$relatedCompany->setUser(null);
}
}
return $this;
}
}
Company.php
<?php
namespace App\Entity;
use App\Repository\CompanyRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=CompanyRepository::class)
*/
class Company
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity=UserCompanyRelation::class, mappedBy="company")
*/
private $relatedUsers;
public function __construct()
{
$this->relatedUsers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* #return Collection|UserCompanyRelation[]
*/
public function getRelatedUsers(): Collection
{
return $this->relatedUsers;
}
public function addRelatedUser(UserCompanyRelation $relatedUser): self
{
if (!$this->relatedUsers->contains($relatedUser)) {
$this->relatedUsers[] = $relatedUser;
$relatedUser->setCompany($this);
}
return $this;
}
public function removeRelatedUser(UserCompanyRelation $relatedUser): self
{
if ($this->relatedUsers->removeElement($relatedUser)) {
// set the owning side to null (unless already changed)
if ($relatedUser->getCompany() === $this) {
$relatedUser->setCompany(null);
}
}
return $this;
}
}
UserCompanyRelation.php
<?php
namespace App\Entity;
use App\Repository\UserCompanyRelationRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=UserCompanyRelationRepository::class)
*/
class UserCompanyRelation
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity=UserCompanyRelationRole::class, inversedBy="userCompanyRelations")
* #ORM\JoinColumn(nullable=false)
*/
private $role;
/**
* #ORM\ManyToOne(targetEntity=Company::class, inversedBy="relatedUsers")
* #ORM\JoinColumn(nullable=false)
*/
private $company;
/**
* #ORM\ManyToOne(targetEntity=User::class, inversedBy="relatedCompanies")
* #ORM\JoinColumn(nullable=false)
*/
private $user;
public function __construct()
{
}
public function getId(): ?int
{
return $this->id;
}
public function getRole(): ?UserCompanyRelationRole
{
return $this->role;
}
public function setRole(?UserCompanyRelationRole $role): self
{
$this->role = $role;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
}
UserCompanyRelationRole.php
<?php
namespace App\Entity;
use App\Repository\UserCompanyRelationRoleRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=UserCompanyRelationRoleRepository::class)
*/
class UserCompanyRelationRole
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity=UserCompanyRelation::class, mappedBy="role")
*/
private $userCompanyRelations;
public function __construct()
{
$this->userCompanyRelations = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* #return Collection|UserCompanyRelation[]
*/
public function getUserCompanyRelations(): Collection
{
return $this->userCompanyRelations;
}
public function addUserCompanyRelation(UserCompanyRelation $userCompanyRelation): self
{
if (!$this->userCompanyRelations->contains($userCompanyRelation)) {
$this->userCompanyRelations[] = $userCompanyRelation;
$userCompanyRelation->setRole($this);
}
return $this;
}
public function removeUserCompanyRelation(UserCompanyRelation $userCompanyRelation): self
{
if ($this->userCompanyRelations->removeElement($userCompanyRelation)) {
// set the owning side to null (unless already changed)
if ($userCompanyRelation->getRole() === $this) {
$userCompanyRelation->setRole(null);
}
}
return $this;
}
}
I found an solution. I had to modify the fetch-strategy to EAGER in a annotation attribute. I modified the relatedUsers annotation property in company.php
<?php
namespace App\Entity;
use App\Repository\CompanyRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=CompanyRepository::class)
*/
class Company
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity=UserCompanyRelation::class, mappedBy="company", fetch="EAGER")
*/
private $relatedUsers;
public function __construct()
{
$this->relatedUsers = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* #return Collection|UserCompanyRelation[]
*/
public function getRelatedUsers(): Collection
{
return $this->relatedUsers;
}
public function addRelatedUser(UserCompanyRelation $relatedUser): self
{
if (!$this->relatedUsers->contains($relatedUser)) {
$this->relatedUsers[] = $relatedUser;
$relatedUser->setCompany($this);
}
return $this;
}
public function removeRelatedUser(UserCompanyRelation $relatedUser): self
{
if ($this->relatedUsers->removeElement($relatedUser)) {
// set the owning side to null (unless already changed)
if ($relatedUser->getCompany() === $this) {
$relatedUser->setCompany(null);
}
}
return $this;
}
}
You should use inversed by for bidirectional mapping between entities. See: https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/association-mapping.html#many-to-many-bidirectional
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 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;
}
}
Then, mine using the same philosophy:
<?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;
}
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 "#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?
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?
You are using
#ORM\nameAgency()
obviously this annotation does not exist, why do you have it there?
Remove #ORM\nameAgency() and #ORM\nameContact, it doesn't make any sense.
Explanation: by #ORM\nameAgency() you actually mean Doctrine\ORM\Mapping\nameAgency and this annotation naturally doesn't exist in Doctrine.