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
Related
I'm having trouble with DQL queries on a project I'm working on. What the DQL is supposed to do is find in the DB all the datas that are related to a productLine variable; that a user would type and search for into a form.
My issue is when I run a test with a product Line; I get an "Error 500" thrown; and I know the error origins from the Repository file; since the controller works fine (I've added some var_dumps in the controller after the query's line to check and I'm not reaching it when testing with the faulty function, whereas with another working function I can reach it).
I know that the data typed in by the user is collected (the product Line entered by the user), there seems to be an issue with DQL processing the query that I can't seem to find an answer for anywhere.
Without further ado; here's the code.
Working fine query
public function findByLotNumber($lotNumber){
return $this->createQueryBuilder('u')
->distinct()
->where('u.lotNumber LIKE :lotNumberS')
->setParameter('lotNumberS', $lotNumber . '%')
->getQuery()
->getResult();
}
faulty query
public function findByCriteriaIdentification($criteriaIdentification){
$givenLotNumber = $_POST['lotNumber'];
if(empty($_POST['lotNumber'])){
$givenLotNumber = '%';
}
$givenProductLine = $_POST['productLine'];
if(empty($_POST['productLine'])){
$givenProductLine = '%';
}
$varTest = $this->createQueryBuilder('u')
->distinct()
->where('u.lotNumber LIKE :lotNumberS')
->orWhere('u.productLine LIKE :productLineS')
->setParameter('lotNumberS', $givenLotNumber)
->setParameter('productLineS', $givenProductLine);
$queryTest = $varTest->getQuery();
return $queryTest->execute();
}
Does anyone have an insight regarding what I'm coding wrong ?
PS: this being a corporate project I don't have the possibility of upgrading to Symfo 6 or Doctrine3 at the moment.
EDIT: Here's the class of the entity i'm querying:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\WafRepository")
*/
class Waf extends Sil
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
* #ORM\OneToOne(targetEntity="App\Entity\Sil")
*/
private $id;
/**
* #ORM\Column(type="integer")
*/
private $rankId;
/**
* #ORM\Column(type="integer", nullable=true)
*/
private $size;
/**
* #ORM\Column(type="integer", nullable=true)
*/
private $thickness;
/*public function getId(): ?int
{
return $this->id;
}*/
public function getRankId(): ?int
{
return $this->rankId;
}
public function setRankId(int $rankId): self
{
$this->rankId = $rankId;
return $this;
}
public function getSize(): ?int
{
return $this->size;
}
public function setSize(int $size): self
{
$this->size = $size;
return $this;
}
public function getThickness(): ?int
{
return $this->thickness;
}
public function setThickness(?int $thickness): self
{
$this->thickness = $thickness;
return $this;
}
}
In case its useful, I'm adding as well the class "sili" that waf is an extent from:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\SiliconRepository")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="silType", type="string")
*/
class Sil
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=20)
*/
private $lotNumber;
/**
* #ORM\Column(type="string", length=80, nullable=true)
*/
private $productLine;
/**
* #ORM\Column(type="string", length=20, nullable=true)
*/
private $productCode;
/**
* #ORM\Column(type="string", length=20, nullable=true)
*/
private $productLineCode;
/**
* #ORM\Column(type="string", length=20, nullable=true)
*/
private $sourceLot;
/**
* #ORM\Column(type="integer", nullable=true)
*/
private $division;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
//private $comment;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $testStatus;
/**
* #ORM\Column(type="integer", nullable=true)
*/
//private $lastUpdate;
/**
* #ORM\Column(type="integer", nullable=true)
*/
private $maturity;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Operations", mappedBy="silicon")
*/
private $operations;
/**
* #ORM\OneToMany(targetEntity="App\Entity\State", mappedBy="silicon")
*/
private $states;
/**
* #ORM\Column(type="string", length=20, nullable=true)
*/
private $fatherLotNumber;
public function __construct()
{
$this->operations = new ArrayCollection();
$this->states = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLotNumber(): ?string
{
return $this->lotNumber;
}
public function setLotNumber(string $lotNumber): self
{
$this->lotNumber = $lotNumber;
return $this;
}
public function getProductLine(): ?string
{
return $this->productLine;
}
public function setProductLine(?string $productLine): self
{
$this->productLine = $productLine;
return $this;
}
public function getProductCode(): ?string
{
return $this->productCode;
}
public function setProductCode(?string $productCode): self
{
$this->productCode = $productCode;
return $this;
}
public function getProductLineCode(): ?string
{
return $this->productLineCode;
}
public function setProductLineCode(?string $productLineCode): self
{
$this->productLineCode = $productLineCode;
return $this;
}
public function getSourceLot(): ?string
{
return $this->sourceLot;
}
public function setSourceLot(?string $sourceLot): self
{
$this->sourceLot = $sourceLot;
return $this;
}
public function getDivision(): ?int
{
return $this->division;
}
public function setDivision(?int $division): self
{
$this->division = $division;
return $this;
}
/*public function getComment(): ?string
{
return $this->comment;
}
public function setComment(?string $comment): self
{
$this->comment = $comment;
return $this;
}*/
public function getTestStatus(): ?string
{
return $this->testStatus;
}
public function setTestStatus(?string $testStatus): self
{
$this->testStatus = $testStatus;
return $this;
}
/*public function getLastUpdate(): ?int
{
return $this->lastUpdate;
}
public function setLastUpdate(int $lastUpdate): self
{
$this->lastUpdate = $lastUpdate;
return $this;
}*/
public function getMaturity(): ?int
{
return $this->maturity;
}
public function setMaturity(?int $maturity): self
{
$this->maturity = $maturity;
return $this;
}
/**
* #return Collection|Operations[]
*/
public function getOperations(): Collection
{
return $this->operations;
}
public function addOperation(Operations $operation): self
{
while (!($this)->operations->contains($operation)) {
$this->operations[] = $operation;
$operation->setSilicon($this);
}
return $this;
}
public function removeOperation(Operations $operation): self
{
if ($this->operations->contains($operation)) {
$this->operations->removeElement($operation);
//$this->reformatTab($this->operations);
// set the owning side to null (unless already changed)
if ($operation->getSilicon() === $this) {
$operation->setSilicon(null);
}
}
return $this;
}
/**
* #return Collection|State[]
*/
public function getStates(): Collection
{
return $this->states;
}
public function addState(State $state): self
{
if (!$this->states->contains($state)) {
$this->states[] = $state;
$state->setSilicon($this);
}
return $this;
}
public function removeState(State $state): self
{
if ($this->states->contains($state)) {
$this->states->removeElement($state);
// set the owning side to null (unless already changed)
if ($state->getSilicon() === $this) {
$state->setSilicon(null);
}
}
return $this;
}
public function getFatherLotNumber(): ?string
{
return $this->fatherLotNumber;
}
public function setFatherLotNumber(?string $fatherLotNumber): self
{
$this->fatherLotNumber = $fatherLotNumber;
return $this;
}
}
Thanks in advance !
I have 2 classes , Class Child "Livreur" inherite from Class Parent "Users" .
Problem in return data after insert by API Plateform . (it inserted by success but data deosn't returned + doesn't insert in table Users, before that it inserted in Users+Livreur but now no ).
Class "Users" (parent) :
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\UsersRepository;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping ;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass=UsersRepository::class)
* #ApiResource(
* normalizationContext={"groups"={"read"}},
* collectionOperations={"post"={},"get"={}},
* itemOperations={"get","put"={"denormalization_Context"={"groups"={"put"}}}}
* )
*/
class Users implements UserInterface
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=180)
* #Groups({"read"})
*/
private $email;
/**
* #Assert\NotBlank()
* #ORM\Column(type="string", length=255)
* #Groups({"read"})
* #Groups({"put"})
*/
private $password;
/**
* #Assert\NotBlank()
* #Assert\Expression("this.getPassword() == this.getRepassword()",message="Mot de pass doit etre le meme dans les 2 deux champs")
*/
private $repassword;
/**
* #Assert\NotBlank()
* #ORM\Column(type="string", length=30)
* #Groups({"read"})
* #Groups({"put"})
*/
private $username;
/**
* #Assert\NotBlank()
* #ORM\Column(type="text")
* #Groups({"read"})
* #Groups({"put"})
*/
private $roles;
/**
* #Assert\NotBlank()
* #ORM\Column(type="string", length=20)
* #Groups({"read"})
*/
private $cin;
/**
* #Assert\NotBlank()
* #ORM\Column(type="string", length=20)
* #Groups({"read"})
*/
private $nom;
/**
* #Assert\NotBlank()
* #ORM\Column(type="string", length=20)
* #Groups({"read"})
*/
private $prenom;
/**
* #Assert\NotBlank()
* #ORM\Column(type="date")
* #Groups({"read"})
*/
private $dtn;
/**
* #Assert\NotBlank()
* #ORM\Column(type="string", length=255, nullable=true)
* #Groups({"read"})
*/
private $dtype;
/**
* #Assert\NotBlank()
* #ORM\Column(type="string", length=255, nullable=true)
* #Groups({"read"})
*/
private $img;
/**
* #Assert\NotBlank()
* #ORM\Column(type="string", length=30, nullable=true)
* #Groups({"read"})
* #Groups({"put"})
*/
private $rib;
/**
* #ORM\Column(type="string", length=255, nullable=true)
* #Groups({"read"})
* #Groups({"put"})
*/
private $adresse;
/**
* #Assert\NotBlank()
* #ORM\Column(type="string", length=20)
* #Groups({"read"})
* #Groups({"put"})
*/
private $tel;
protected function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getRoles(): array
{
return array('ROLE_USER');
}
public function setRoles(string $roles): self
{
$this->roles = $roles;
return $this;
}
public function getCin(): ?string
{
return $this->cin;
}
public function setCin(string $cin): self
{
$this->cin = $cin;
return $this;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
public function getPrenom(): ?string
{
return $this->prenom;
}
public function setPrenom(string $prenom): self
{
$this->prenom = $prenom;
return $this;
}
public function getDtn(): ?\DateTimeInterface
{
return $this->dtn;
}
public function setDtn(\DateTimeInterface $dtn): self
{
$this->dtn = $dtn;
return $this;
}
public function getDtype(): ?string
{
return $this->dtype;
}
public function setDtype(?string $dtype): self
{
$this->dtype = $dtype;
return $this;
}
public function getImg(): ?string
{
return $this->img;
}
public function setImg(?string $img): self
{
$this->img = $img;
return $this;
}
public function getRib(): ?string
{
return $this->rib;
}
public function setRib(?string $rib): self
{
$this->rib = $rib;
return $this;
}
public function getAdresse(): ?string
{
return $this->adresse;
}
public function setAdresse(?string $adresse): self
{
$this->adresse = $adresse;
return $this;
}
public function getTel(): ?string
{
return $this->tel;
}
public function setTel(string $tel): self
{
$this->tel = $tel;
return $this;
}
public function getSalt()
{
// TODO: Implement getSalt() method.
}
public function eraseCredentials()
{
// TODO: Implement eraseCredentials() method.
}
public function getRepassword()
{
return $this->repassword;
}
public function setRepassword($repassword): void
{
$this->repassword = $repassword;
}
}
Class Livreur (child) :
<?php
namespace App\Entity;
use App\Repository\LivreurRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiResource;
/**
* #ORM\Entity(repositoryClass=LivreurRepository::class)
* #ApiResource()
*/
class Livreur extends Users
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=20)
*/
private $type_vehicule;
/**
* #ORM\Column(type="string", length=20)
*/
private $permis;
/**
* #ORM\Column(type="boolean")
*/
private $disponibilite;
/**
* #ORM\Column(type="float")
*/
private $coffre;
/**
* #ORM\Column(type="text", nullable=true)
*/
private $log;
/**
* #ORM\OneToMany(targetEntity=Commande::class, mappedBy="livreur")
*/
private $commandes;
/**
* #ORM\OneToMany(targetEntity=Conge::class, mappedBy="livreur")
*/
private $conges;
/**
* #ORM\ManyToOne(targetEntity=Agence::class, inversedBy="livreurs")
* #ORM\JoinColumn(nullable=false)
*/
private $agence;
/**
* #ORM\OneToOne(targetEntity=SalaireEmp::class, inversedBy="livreur", cascade={"persist", "remove"})
*/
private $salaire_emp;
/**
* #ORM\ManyToOne(targetEntity=Ville::class, inversedBy="livreurs")
* #ORM\JoinColumn(nullable=false)
*/
private $ville;
public function __construct()
{
$this->commandes = new ArrayCollection();
$this->conges = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTypeVehicule(): ?string
{
return $this->type_vehicule;
}
public function setTypeVehicule(string $type_vehicule): self
{
$this->type_vehicule = $type_vehicule;
return $this;
}
public function getPermis(): ?string
{
return $this->permis;
}
public function setPermis(string $permis): self
{
$this->permis = $permis;
return $this;
}
public function getDisponibilite(): ?int
{
return $this->disponibilite;
}
public function setDisponibilite(int $disponibilite): self
{
$this->disponibilite = $disponibilite;
return $this;
}
public function getCoffre(): ?float
{
return $this->coffre;
}
public function setCoffre(float $coffre): self
{
$this->coffre = $coffre;
return $this;
}
public function getLog(): ?string
{
return $this->log;
}
public function setLog(?string $log): self
{
$this->log = $log;
return $this;
}
/**
* #return Collection|Commande[]
*/
public function getCommandes(): Collection
{
return $this->commandes;
}
public function addCommande(Commande $commande): self
{
if (!$this->commandes->contains($commande)) {
$this->commandes[] = $commande;
$commande->setLivreur($this);
}
return $this;
}
public function removeCommande(Commande $commande): self
{
if ($this->commandes->removeElement($commande)) {
// set the owning side to null (unless already changed)
if ($commande->getLivreur() === $this) {
$commande->setLivreur(null);
}
}
return $this;
}
/**
* #return Collection|Conge[]
*/
public function getConges(): Collection
{
return $this->conges;
}
public function addConge(Conge $conge): self
{
if (!$this->conges->contains($conge)) {
$this->conges[] = $conge;
$conge->setLivreur($this);
}
return $this;
}
public function removeConge(Conge $conge): self
{
if ($this->conges->removeElement($conge)) {
// set the owning side to null (unless already changed)
if ($conge->getLivreur() === $this) {
$conge->setLivreur(null);
}
}
return $this;
}
public function getAgence(): ?Agence
{
return $this->agence;
}
public function setAgence(?Agence $agence): self
{
$this->agence = $agence;
return $this;
}
public function getSalaireEmp(): ?SalaireEmp
{
return $this->salaire_emp;
}
public function setSalaireEmp(?SalaireEmp $salaire_emp): self
{
$this->salaire_emp = $salaire_emp;
return $this;
}
public function getVille(): ?Ville
{
return $this->ville;
}
public function setVille(?Ville $ville): self
{
$this->ville = $ville;
return $this;
}
}
Looks like you forgot to set an inheritance mapping strategy. Here is an example:
// (..)
/**
* #ORM\Entity(repositoryClass=UsersRepository::class)
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({"users" = "Users", "livreur" = "Livreur"})
* #ApiResource(
* normalizationContext={"groups"={"read"}},
* collectionOperations={"post"={},"get"={}},
* itemOperations={"get","put"={"denormalization_Context"={"groups"={"put"}}}}
* )
*/
class Users implements UserInterface
{
// (..)
This will add an extra field "discr" to the tables of both your classes so you will have to update their table configurations, for example with:
bin/console doctrine:schema:update --force
or if you use migrations:
bin/console doctrine:migrations:diff
bin/console doctrine:migrations:migrate
For explanation of Mapping Strategies and their alternatives see the Inheritance Mapping page on doctrine-project.org
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.
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.
I made this for GET statement in the controller
<?php
namespace App\Controller\Api;
use App\Entity\Article;
use App\Factory\EntityFactory;
use App\Repository\ArticleRepository;
use Swagger\Annotations as SWG;
use Nelmio\ApiDocBundle\Annotation\Model;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Routing\Annotation\Route;
class ArticlesController
{
private $repository;
private $factory;
private $serializer;
public function __construct(ArticleRepository $repository, EntityFactory $entityFactory, SerializerInterface $serializer)
{
$this->repository = $repository;
$this->factory = $entityFactory;
$this->serializer = $serializer;
}
/**
* #Route(path="/articles/{article}", methods={"GET"}, name="article_get")
*
* #SWG\Get(
* tags={"Articles"},
* #SWG\Parameter(
* name="article",
* in="path",
* type="integer",
* description="Article ID",
* ),
* )
* #SWG\Response(
* response=200,
* description="Article fetched",
* #Model(type=Article::class, groups={"article:get", "article:category", "category:index"})
* )
*/
public function get(Article $article): JsonResponse
{
$data = $this->serializer->serialize($article, 'json', ['groups' => ['article:get', 'article:category', 'category:index']]);
return new JsonResponse($data, JsonResponse::HTTP_OK, [], true);
}
}
The entity from the article looks like this:
<?php
namespace App\Entity;
use App\Entity\User\User;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Swagger\Annotations as SWG;
/**
* #ORM\Entity(repositoryClass="App\Repository\ArticleRepository")
* #ORM\Table(name="articles")
* #ORM\HasLifecycleCallbacks
*/
class Article implements EntityInterface
{
/**
* #var int
*
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
* #Groups({"article:id", "article:get", "article:index"})
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
* #Groups({"article:category"})
*
*/
private $title;
/**
* #ORM\Column(type="string", length=255)
* #Groups({"article:create", "article:get", "article:index", "article:update"})
*/
private $lead;
/**
* #ORM\Column(type="string", length=255, nullable=true)
* #Groups({"article:create", "article:get", "article:index", "article:update"})
*/
private $slug;
/**
* #ORM\Column(type="text", nullable=true)
* #Groups({"article:create", "article:get", "article:index", "article:update"})
*/
private $content;
/**
* #ORM\Column(type="datetime")
* #Groups({"article:create", "article:get", "article:index", "article:update"})
* #SWG\Property(property="updated_at")
*/
private $publishedAt;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="articles")
* #ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE", nullable=false)
* #Groups({"article:category"})
*
*/
private $category;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User\User", inversedBy="articles")
* #ORM\JoinColumn(nullable=false)
* #Groups({"article:user"})
*/
private $author;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $articleFilename;
/**
* #ORM\Column(type="json_array", nullable=true)
*/
private $marking;
/**
* #ORM\Column(type="json_array", nullable=true)
*/
private $transitionContexts;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getLead(): ?string
{
return $this->lead;
}
public function setLead(string $lead): self
{
$this->lead = $lead;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getPublishedAt(): ?\DateTimeInterface
{
return $this->publishedAt;
}
public function setPublishedAt(\DateTimeInterface $publishedAt): self
{
$this->publishedAt = $publishedAt;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
public function getAuthor(): ?User
{
return $this->author;
}
public function setAuthor(?User $author): self
{
$this->author = $author;
return $this;
}
public function getArticleFilename(): ?string
{
return $this->articleFilename;
}
public function setArticleFilename(?string $articleFilename): self
{
$this->articleFilename = $articleFilename;
return $this;
}
public function getImagePath()
{
return 'images/'.$this->getImageFilename();
}
public function getMarking()
{
return $this->marking;
}
public function setMarking($marking, $context = [])
{
$this->marking = $marking;
$this->transitionContexts[] = [
'new_marking' => $marking,
'context' => $context,
'time' => (new \DateTime())->format('c'),
];
}
public function getTransitionContexts()
{
return $this->transitionContexts;
}
public function setTransitionContexts($transitionContexts): self
{
$this->transitionContexts = $transitionContexts;
}
}
And when I go to http://localhost/docs I become this error. That my annotations are not enabled or installed. Thank you for your help.
Error message:
Exception thrown when handling an exception
(Symfony\Component\Config\Exception\LoaderLoadException: [Syntax
Error] Expected
Doctrine\Common\Annotations\DocLexer::T_CLOSE_CURLY_BRACES, got
'article' at position 179 in method
App\Controller\Api\ArticlesController::get() in
/app/config/routes/../../src/Controller/Api (which is being imported
from "/app/config/routes/annotations.yaml"). Make sure annotations are
installed and enabled.)