Missing hydra:total items in api - php

I'm currently having an issue I do not manage to explain.
I have a symfony/api-platform entity defined like this:
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* #ORM\Entity
* #ApiResource(
* normalizationContext={"groups"={"node_read"}},
* denormalizationContext={"groups"={"node_write"}},
* collectionOperations={
* "get"={"method"="GET"},
* "post"={"method"="POST", "security"="is_granted('ROLE_ADMIN')"}
* },
* itemOperations={
* "get"={"method"="GET"},
* "put"={"method"="PUT", "security"="is_granted('ROLE_ADMIN')"},
* "patch"={"method"="PATCH", "security"="is_granted('ROLE_ADMIN')"},
* "delete"={"method"="DELETE", "security"="is_granted('ROLE_ADMIN')"}
* }
* )
* #ApiFilter(ExistsFilter::class, properties={"parent"})
*/
class Node
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
* #Groups({"node_read"})
*/
private ?int $id;
/**
* #ORM\Column(type="string", length=255)
* #Groups({"node_read", "node_write"})
*/
private string $key;
/**
* #ORM\Column(type="string", length=255)
* #Groups({"node_read", "node_write"})
*/
private string $label;
/**
* #ORM\Column(type="string", length=255)
* #Groups({"node_read", "node_write"})
*/
private string $type;
/**
* One Category has Many Categories.
*
* #OneToMany(targetEntity="Node", mappedBy="parent")
* #Groups({"node_read"})
*/
private Collection $children;
/**
* Many Nodes have One Node.
*
* #ManyToOne(targetEntity="Node", inversedBy="children")
* #JoinColumn(name="parent_id", referencedColumnName="id")
* #Groups({"node_write"})
*/
private ?Node $parent;
/**
* #ORM\Column(type="string", nullable=true)
*/
private string $description;
public function __construct()
{
$this->children = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getParent(): ?Node
{
return $this->parent;
}
public function setParent(?Node $parent): self
{
$this->parent = $parent;
return $this;
}
public function getChildren(): Collection
{
return $this->children;
}
public function setChildren(Collection $children): self
{
$this->children = $children;
return $this;
}
public function getKey(): string
{
return $this->key;
}
public function setKey(string $key): self
{
$this->key = $key;
return $this;
}
public function getLabel(): string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getType(): string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getDescription(): string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
}
While making a get call on the collection, I have different results between development environment and production.
In the prod env, the api is missing a hydra metadata key hydra:total.
Results in dev env :
context: "/api/contexts/Node"
#id: "/api/nodes"
#type: "hydra:Collection"
hydra:member: [{#id: "/api/nodes/1", #type: "Node", id: 1, key: "risk-of-rain-2", label: "Risk Of Rain 2",…},…]
hydra:totalItems: 2
hydra:view: {#id: "/api/nodes?exists%5Bparent%5D=false", #type: "hydra:PartialCollectionView"}
hydra:search: {#type: "hydra:IriTemplate", hydra:template: "/api/nodes{?exists[parent]}",…}
results in prod env :
#id: "/api/nodes"
#type: "hydra:Collection"
hydra:member: [{#id: "/api/nodes/12", #type: "Node", id: 12, key: "feedback", label: "Feedback", type: "Game",…},…]
hydra:view: {#id: "/api/nodes?exists%5Bparent%5D=false", #type: "hydra:PartialCollectionView"}
hydra:search: {#type: "hydra:IriTemplate", hydra:template: "/api/nodes{?exists[parent]}",…}
As you can see in prod I am missimg the key hydra:totalItems: 2 which allows me to make react-admin work.
I have compared both project composer dependency and PHP version and prod is ISO to dev env.

I found my issue which was not related at all with the provided source code but with deployment.
Rsync did not remove deleted file of the project and I had an overridding Dataprovider.
Solution was to use --delete option in rsync command.

Related

Column name referenced for relation not exist - ManyToMany relationship Symfony Doctrine

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;
}
}

Symfony API platform return IRI even when using Groups annotations

I there,
I'm trying to use api platform with symfony 4 but I'm unable to load related entities of an object even using normalizationContext and Groups.
For example, I have 2 classes FieldType and Field. Each Field have a single one FieldType. To load the Field with the fieldtype I created the "field" group which I use in Field to load all the propeties and also in FieldType to also load the field type properties. But at the end I still get the IRI in the returned JSON.
Can someone help me to figure out what is wrong?
Here is the code for the 2 classes:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\AUDFieldRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiSubresource;
/**
* #ApiResource(
* normalizationContext={"groups"={"field:read"}},
* )
* #ORM\Entity(repositoryClass=AUDFieldRepository::class)
* #ORM\Table(name="aud_field")
*/
class AUDField
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
* #Groups("field:read")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, unique=true)
* #Groups({"field:read"})
*/
private $name;
/**
* #ORM\Column(type="boolean")
*/
private $optional = false;
/**
* #ORM\Column(type="string", length=40, nullable=true)
* #Groups({"field:read"})
*/
private $inputType;
/**
* #ORM\Column(type="text", nullable=true)
* #Groups({"field:read"})
*/
private $inputValues;
/**
* #var array
* #ORM\Column(type="json_array", nullable=true)
* #Groups({"field:read"})
*/
private $specifications;
/**
* #ORM\ManyToOne(targetEntity=AUDFieldType::class)
* #ORM\JoinColumn(nullable=false)
* #Groups({"field:read"})
*/
private $type;
/**
* #ORM\ManyToMany(targetEntity=AUDAttributeType::class, mappedBy="fields")
* #Groups({"field:read"})
*/
private $attributesTypes;
public function __construct()
{
$this->attributesTypes = 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;
}
public function getOptional(): bool
{
return $this->optional;
}
public function isOptional(): bool
{
return $this->optional;
}
public function setOptional(bool $bool): self
{
$this->optional = $bool;
return $this;
}
public function getSpecifications(): ?array
{
return $this->specifications;
}
public function getSpecification($key): ?array
{
return json_decode($this->specifications[$key]);
}
public function setSpecifications(?array $specs): self
{
$this->specifications = $specs;
return $this;
}
public function setSpecification($key, $value): ?array
{
return $this->specifications[$key] = $value;
}
public function hasSpecification($key): bool
{
return array_key_exists($key, $this->specifications);
}
/*public function addSpecification($key, $value): self
{
$this->specifications[$key] = $value;
return $this;
}
public function removeSpecification($key): self
{
unset($this->specifications[$key]);
return $this;
}*/
public function getInputValues(): ?string
{
return $this->inputValues;
}
public function setInputValues(?string $inputValues): self
{
$this->inputValues = $inputValues;
return $this;
}
public function getInputType(): ?string
{
return $this->inputType;
}
public function setInputType(?string $inputType): self
{
$this->inputType = $inputType;
return $this;
}
public function getType(): ?AUDFieldType
{
return $this->type;
}
public function setType(?AUDFieldType $type): self
{
$this->type = $type;
return $this;
}
/**
* #return Collection|AUDAttributeType[]
*/
public function getAttributesTypes(): Collection
{
return $this->attributesTypes;
}
public function addAttributesType(AUDAttributeType $attributesType): self
{
if (!$this->attributesTypes->contains($attributesType)) {
$this->attributesTypes[] = $attributesType;
$attributesType->addField($this);
}
return $this;
}
public function removeAttributesType(AUDAttributeType $attributesType): self
{
if ($this->attributesTypes->removeElement($attributesType)) {
$attributesType->removeField($this);
}
return $this;
}
}
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Repository\AUDFieldTypeRepository;
/**
* #ApiResource(normalizationContext={"groups"={"field:read"}})
* #ORM\Entity(repositoryClass=AUDFieldTypeRepository::class)
* #ORM\Table(name="aud_field_type")
*/
class AUDFieldType
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
* #Groups({"field:read"})
*/
private $id;
/**
* #ORM\Column(type="string", length=100)
* #Groups({"field:read"})
*/
private $name;
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;
}
}
End the Postman output for http://127.0.0.1:8000/api/field/1:
{
"#context": "/api/contexts/AUDField",
"#id": "/api/field/1",
"#type": "AUDField",
"id": 1,
"name": "Identifiant",
"specifications": {
"minlength": 4
},
"type": "/api/fieldtype/1",
"attributesTypes": [
"/api/attributetype/1"
]
}
And for http://127.0.0.1:8000/api/fieldtype/1:
{
"#context": "/api/contexts/AUDFieldType",
"#id": "/api/fieldtype/1",
"#type": "AUDFieldType"
}
Try to remove normalizationContext in AUDFieldType class or rename it with field-type:read.

php bin/console doctrine:migrations:migrate doesn't work

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

Relation One to Many return empty object

I have a Evaluation entity which has one Product and Product which can have several Evaluations. I'm trying to fetch one Product and to get the list of Evaluations associated with my entity
Produit.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use phpDocumentor\Reflection\Types\This;
/**
* Produit
*
* #ORM\Table(name="produit", indexes={#ORM\Index(name="fk_idcatedel", columns={"idCategorie"})})
* #ORM\Entity
*/
class Produit
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string|null
*
* #ORM\Column(name="libelle", type="string", length=20, nullable=true)
*/
private $libelle;
/**
* #var float|null
*
* #ORM\Column(name="prix", type="float", precision=10, scale=0, nullable=true)
*/
private $prix;
/**
* #var string|null
*
* #ORM\Column(name="description", type="string", length=50, nullable=true)
*/
private $description;
/**
* #var int
*
* #ORM\Column(name="qt", type="integer", nullable=false)
*/
private $qt;
/**
* #var string|null
*
* #ORM\Column(name="img", type="string", length=255, nullable=true)
*/
private $img;
/**
* #var \Categorie
*
* #ORM\ManyToOne(targetEntity="Categorie")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="idCategorie", referencedColumnName="id")
* })
*/
private $idcategorie;
/**
* #ORM\OneToMany(targetEntity="Evaluation", mappedBy="idProduit")
*/
private $rates;
public function __construct()
{
$this->rates = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLibelle(): ?string
{
return $this->libelle;
}
public function setLibelle(?string $libelle): self
{
$this->libelle = $libelle;
return $this;
}
public function getPrix(): ?float
{
return $this->prix;
}
public function setPrix(?float $prix): self
{
$this->prix = $prix;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getQt(): ?int
{
return $this->qt;
}
public function setQt(int $qt): self
{
$this->qt = $qt;
return $this;
}
public function getImg(): ?string
{
return $this->img;
}
public function setImg(?string $img): self
{
$this->img = $img;
return $this;
}
public function getIdcategorie(): ?Categorie
{
return $this->idcategorie;
}
public function setIdcategorie(?Categorie $idcategorie): self
{
$this->idcategorie = $idcategorie;
return $this;
}
/**
* #return Collection|Evaluation[]
*/
public function getRates(): Collection
{
return $this->rates;
}
}
Evaluation.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Evaluation
*
* #ORM\Table(name="evaluation", indexes={#ORM\Index(name="fk_idprodevaldel", columns={"id_produit"}), #ORM\Index(name="fk_iduser", columns={"id_user"})})
* #ORM\Entity
*/
class Evaluation
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var int
*
* #ORM\Column(name="note", type="integer", nullable=false)
*/
private $note;
/**
* #var \Produit
*
* #ORM\ManyToOne(targetEntity="Produit", inversedBy="rates")
* #ORM\JoinColumn(name="id_produit", referencedColumnName="id")
*/
private $idProduit;
/**
* #var \Compte
*
* #ORM\ManyToOne(targetEntity="Compte")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_user", referencedColumnName="email")
* })
*/
private $idUser;
public function getId(): ?int
{
return $this->id;
}
public function getNote(): ?int
{
return $this->note;
}
public function setNote(int $note): self
{
$this->note = $note;
return $this;
}
public function getIdProduit(): ?Produit
{
return $this->idProduit;
}
public function setIdProduit(?Produit $idProduit): self
{
$this->idProduit = $idProduit;
return $this;
}
public function getIdUser(): ?Compte
{
return $this->idUser;
}
public function setIdUser(?Compte $idUser): self
{
$this->idUser = $idUser;
return $this;
}
}
The database
In my controller I succeed to get informations from the products but rates are empty
$produits = $this->getDoctrine()
->getRepository(Produit::class)
->find(1);
dump($produits);
$rates = $produits->getRates();
dump($rates); // #collection: ArrayCollection is empty
The Output :
The collection is not yet initialized due to lazy loading, and rightfully so. If you don't access at least to an element in the collection, it's pointless to load the whole collection because doctrine can safely assume you'll "discard" it. As soon as you access an element (either by looping onto collection or getting a specific element), the collection will be initialized and you have all items.
Another way is to use an EAGER fetch that will load the whole collection in the hydration phase. I would not reccomend it however, unless you're sure that everytime you load a Produit, you need this collection "ready". Even in the latter case, I would handle the collection "manually" as I recommend not to lose control on it (let's pretend you have A LOT of element inside it).
Read more about proxies and association, here

#Doctrine\ORM\Mapping\ "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 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.

Categories