I'm using Symfony 5 with API Platform and Doctrine. I have a "Source" entity and a "SourceContributor" join table, which contains additional info for the relation.
Source:
/**
* ...
*
* #ORM\Entity()
*/
class Source
{
// ...
/**
* #Groups({"source:read", "source:write"})
* #ORM\OneToMany(targetEntity="SourceContributor", mappedBy="source", cascade={"persist", "merge"}, orphanRemoval=true)
*/
private Collection $contributors;
public function getContributors(): Collection
{
return $this->contributors;
}
public function addContributor(SourceContributor $contributor): self
{
if (!$this->contributors->contains($contributor)) {
$contributor->setSource($this);
$this->contributors[] = $contributor;
}
return $this;
}
public function removeContributor(SourceContributor $contributor): self
{
if ($this->contributors->contains($contributor)) {
$this->contributors->removeElement($contributor);
}
return $this;
}
public function __construct()
{
$this->contributors = new ArrayCollection();
}
}
SourceContributor:
/**
* #ORM\Entity
*/
class SourceContributor
{
/**
* #ORM\Id()
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\ManyToOne(targetEntity="Source", inversedBy="contributors")
*/
private ?Source $source;
public function getSource(): ?Source
{
return $this->source;
}
public function setSource(?Source $source): self
{
$this->source = $source;
return $this;
}
/**
* #Groups({"source:read", "source:write"})
* #ORM\Id()
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\ManyToOne(targetEntity="Contributor")
*/
private ?Contributor $contributor;
public function getContributor(): ?Contributor
{
return $this->contributor;
}
public function setContributor(?Contributor $contributor): self
{
$this->contributor = $contributor;
return $this;
}
/**
* #Groups({"source:read", "source:write"})
* #ORM\Id()
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\Column(type="smallint", nullable=false, options={"unsigned"=true})
*/
private ?int $role;
public function getRole(): ?int
{
return $this->role;
}
public function setRole(int $role): self
{
$this->role = $role;
return $this;
}
}
Creating my Source entity via POST like this works fine:
{
"contributors": [
{
"contributor": "/contributors/1",
"role": 0
},
{
"contributor": "/contributors/1",
"role": 1
}
]
}
However, after the entity is created, updating it the same way via PUT does not work.
An exception occurred while executing 'INSERT INTO source_contributor
(role, source_id, contributor_id) VALUES (?, ?, ?)' with params [0, 4,
1]:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate
entry '4-1-0' for key 'PRIMARY'
Doctrine seems to be trying to insert the SourceContributor again.
Does anyone know a solution?
It happens because you pass an object, if you don't need to create a new source_contributor, you have to pass a URI.
Replace
{
"contributors": [
{
"contributor": "/contributors/1",
"role": 0
}
]
}
to:
{
"contributors": [
"/contributors/1"
]
}
Related
I can't use group annotation to get only values who I decide to show. In my exemple, I want to get only emails of my users when I show all users with collectionOperations. It's the same problem with itemOperations
Here is my entity:
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* #ORM\Entity(repositoryClass=UserRepository::class)
* #ApiResource(
* collectionOperations={
* "post",
* "get"={
* "normalization_context"={"groups"={"user:read"}}
* }
* }
* )
*/
class User implements UserInterface
{
use ResourceId;
use Timestamble;
/**
* #ORM\Column(type="string", length=180, unique=true)
* #Groups("user:read")
*
*/
private $email;
/**
* #ORM\Column(type="json")
*/
private $roles = [];
/**
* #var string The hashed password
* #ORM\Column(type="string")
*/
private $password;
/**
* #ORM\OneToMany(targetEntity=Article::class, mappedBy="author", orphanRemoval=true)
*/
private $articles;
public function __construct()
{
$this->articles = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
}
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->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* #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;
}
/**
* #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->setAuthor($this);
}
return $this;
}
public function removeArticle(Article $article): self
{
if ($this->articles->contains($article)) {
$this->articles->removeElement($article);
// set the owning side to null (unless already changed)
if ($article->getAuthor() === $this) {
$article->setAuthor(null);
}
}
return $this;
}
}
My API should return only email when I get in all my users, but returns:
"#context": "/api/contexts/User",
"#id": "/api/users",
"#type": "hydra:Collection",
"hydra:member": [
{
"#id": "/api/users/1",
"#type": "User"
},
{
"#id": "/api/users/2",
"#type": "User"
},
{
"#id": "/api/users/3",
"#type": "User"
},
// ...
],
"hydra:totalItems": 10
}
I don't understand why my emails are not visible, the collectionOperations works, the problem comes to #Groups("user:read") which isn't detected.
just replace this
* #Groups("user:read")
by this:
* #Groups({"user:read"})
and don't forget to clear the cache each time you modify the metadata
I have an intermediate table that acts as a pivot between two others: user, punto_suministro and table pivot user_punto_suministro:
How to fetch data from table user to table punto_suministro?
Entity User:
/**
* #ORM\Entity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
.....
/**
* #ORM\OneToMany(targetEntity=UserPuntoSuministro::class, mappedBy="user")
*/
private $userPuntoSuministros;
/**
* #throws \Exception
*/
public function __construct()
{
$this->puntoSuministros = new ArrayCollection();
$this->userPuntoSuministros = new ArrayCollection();
}
/**
* #return Collection|UserPuntoSuministro[]
*/
public function getUserPuntoSuministros(): Collection
{
return $this->userPuntoSuministros;
}
public function addUserPuntoSuministro(UserPuntoSuministro $userPuntoSuministro): self
{
if (!$this->userPuntoSuministros->contains($userPuntoSuministro)) {
$this->userPuntoSuministros[] = $userPuntoSuministro;
$userPuntoSuministro->setUser($this);
}
return $this;
}
public function removeUserPuntoSuministro(UserPuntoSuministro $userPuntoSuministro): self
{
if ($this->userPuntoSuministros->contains($userPuntoSuministro)) {
$this->userPuntoSuministros->removeElement($userPuntoSuministro);
// set the owning side to null (unless already changed)
if ($userPuntoSuministro->getUser() === $this) {
$userPuntoSuministro->setUser(null);
}
}
return $this;
}
Entity PuntoSuministro:
/**
* #ORM\Entity(repositoryClass=PuntoSuministroRepository::class)
*/
class PuntoSuministro
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
...
/**
* #ORM\OneToMany(targetEntity=UserPuntoSuministro::class, mappedBy="punto_suministro")
*/
private $userPuntoSuministros;
public function __construct()
{
$this->userPuntoSuministros = new ArrayCollection();
}
/**
* #return Collection|UserPuntoSuministro[]
*/
public function getUserPuntoSuministros(): Collection
{
return $this->userPuntoSuministros;
}
public function addUserPuntoSuministro(UserPuntoSuministro $userPuntoSuministro): self
{
if (!$this->userPuntoSuministros->contains($userPuntoSuministro)) {
$this->userPuntoSuministros[] = $userPuntoSuministro;
$userPuntoSuministro->setPuntoSuministro($this);
}
return $this;
}
public function removeUserPuntoSuministro(UserPuntoSuministro $userPuntoSuministro): self
{
if ($this->userPuntoSuministros->contains($userPuntoSuministro)) {
$this->userPuntoSuministros->removeElement($userPuntoSuministro);
// set the owning side to null (unless already changed)
if ($userPuntoSuministro->getPuntoSuministro() === $this) {
$userPuntoSuministro->setPuntoSuministro(null);
}
}
return $this;
}
Entity UserPuntoSuministro (table pivot):
/**
* #ORM\Entity(repositoryClass=UserPuntoSuministroRepository::class)
*/
class UserPuntoSuministro
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity=User::class, inversedBy="userPuntoSuministros")
*/
private $user;
/**
* #ORM\ManyToOne(targetEntity=PuntoSuministro::class, inversedBy="userPuntoSuministros")
*/
private $punto_suministro;
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getPuntoSuministro(): ?PuntoSuministro
{
return $this->punto_suministro;
}
public function setPuntoSuministro(?PuntoSuministro $punto_suministro): self
{
$this->punto_suministro = $punto_suministro;
return $this;
}
I try and access as follows:
$user = $userRepository->find($this->security->getUser());
$pivote = $user->getUserPuntoSuministros();
but from here I don't know how to recover the punto_suministro of a user to show it on twig.
Thanks in advance.
In simple case you could iterate over $user->getUserPuntoSuministros() and fetch concrete punto_suministro's
$user = $userRepository->find($this->security->getUser());
$puntoSuministros = []; // contains PuntoSuministro's of User
foreach ($user->getUserPuntoSuministros() as $userPuntoSuministro) {
$puntoSuministros[] = $userPuntoSuministro->getPuntoSuministro();
}
I'm currently doing app that's similar to imgur and I'm receiving following error
An exception occurred while executing 'INSERT INTO user_gallery_views (gallery_id, user_id) VALUES (?, ?)' with params [1, 1]:
SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: user_gallery_views.gallery_id, user_gallery_views.user_id
It occurs while calling path to add object as saved in users collection
Related part of App Controller
/**
* #Route("/image/{id}", name="imgDisp", methods={"GET"})
*/
public function imgDisp($id)
{
$img = $this->gR->findBy(['id'=>$id]);
if ($img) {
$img = $img[0];
$img->setImage(stream_get_contents($img->getImage()));
}
if($this->ls)
{
$this->viewMod($id);
}
return $this->render('app/imgDisp.html.twig', [
'img'=>$img
]);
}
/**
* #Route("/image/{id}/like", name="likeImg", methods={"POST"})
*/
public function likeImg($id)
{
$img = $this->gR->findBy(['id'=>$id])[0];
$user = $this->uR->findBy(['id'=>$this->session->get('user')->getId()])[0];
if(!$img->getLikes()->contains($user))
{
$img->addLike($user);
}
else
{
$img->removeLike($user);
}
$this->em->flush();
return $this->redirectToRoute('imgDisp', ['id'=>$id]);
}
/**
* #Route("/image/{id}/save", name="saveImg", methods={"POST"})
*/
public function saveImg($id)
{
$img = $this->gR->findBy(['id'=>$id])[0];
$user = $this->uR->findBy(['id'=>$this->session->get('user')->getId()])[0];
if(!$img->getSaves()->contains($user))
{
$img->addSave($user);
}
else
{
$img->removeSave($user);
}
$this->em->flush();
return $this->redirectToRoute('imgDisp', ['id'=>$id]);
}
private function viewMod($id)
{
$img = $this->gR->findBy(['id'=>$id])[0];
$user = $this->uR->findBy(['id'=>$this->session->get('user')->getId()])[0];
if(!$img->getViews()->contains($user))
{
$img->addView($user);
$this->em->flush();
}
}
Gallery entity (part related to problem)
/**
* #ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="likes")
* #ORM\JoinTable(name="user_gallery_likes")
*/
private $likes;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="collection")
* #ORM\JoinTable(name="user_gallery_saves")
*/
private $saves;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="views")
* #ORM\JoinTable(name="user_gallery_views")
*/
private $views;
/**
* #return Collection|User[]
*/
public function getLikes(): Collection
{
return $this->likes;
}
public function addLike(User $like): self
{
if (!$this->likes->contains($like)) {
$this->likes[] = $like;
}
return $this;
}
public function removeLike(User $like): self
{
if ($this->likes->contains($like)) {
$this->likes->removeElement($like);
}
return $this;
}
/**
* #return Collection|User[]
*/
public function getSaves(): Collection
{
return $this->saves;
}
public function addSave(User $save): self
{
if (!$this->saves->contains($save)) {
$this->views[] = $save;
}
return $this;
}
public function removeSave(User $save): self
{
if ($this->saves->contains($save)) {
$this->saves->removeElement($save);
}
return $this;
}
/**
* #return Collection|User[]
*/
public function getViews(): Collection
{
return $this->views;
}
public function addView(User $view): self
{
if (!$this->views->contains($view)) {
$this->views[] = $view;
}
return $this;
}
Users entity (part related to problem)
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Gallery", mappedBy="saves")
*/
private $collection;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Gallery", mappedBy="likes")
*/
private $likes;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Gallery", mappedBy="views")
*/
private $views;
/**
* #return Collection|Gallery[]
*/
public function getCollection(): Collection
{
return $this->collection;
}
public function addCollection(Gallery $collection): self
{
if (!$this->collection->contains($collection)) {
$this->collection[] = $collection;
}
return $this;
}
public function removeCollection(Gallery $collection): self
{
if ($this->collection->contains($collection)) {
$this->collection->removeElement($collection);
}
return $this;
}
/**
* #return Collection|Gallery[]
*/
public function getLikes(): Collection
{
return $this->likes;
}
public function addLike(Gallery $like): self
{
if (!$this->likes->contains($like)) {
$this->likes[] = $like;
$like->addLike($this);
}
return $this;
}
public function removeLike(Gallery $like): self
{
if ($this->likes->contains($like)) {
$this->likes->removeElement($like);
$like->removeLike($this);
}
return $this;
}
/**
* #return Collection|Gallery[]
*/
public function getViews(): Collection
{
return $this->views;
}
public function addView(Gallery $view): self
{
if (!$this->views->contains($view)) {
$this->views[] = $view;
$view->addView($this);
}
return $this;
}
The point I don't get is why error concern user_galler_views when it's not even used in /image/{id}/save?
I'm for sure not seeing something but don't even know what, so I'm full of hope u gonna help me
It looks like you are storing the entity to the wrong Array.
public function addSave(User $save): self
{
if (!$this->saves->contains($save)) {
$this->views[] = $save;
}
return $this;
}
You check for $this->saves->contains($save) but then you store the data not to saves but to views.
$this->views[] = $save;
It is probably a coincidence that the save entity has the same id as another view entity that is already assigned to the gallery.
I'm struggling for a while here. I need to display a table with some data. I've to get this data from different entities. I can't to get one value correctly as my join seems to break to correct data.
He is my Repository function :
$qb->select('j AS jou')
->innerJoin('j.playeds', 'p')
->addSelect('SUM(p.points) AS sumpoints')
->addSelect('SUM(p.max) AS summax')
->addSelect('COUNT(p.partie) as sumparties')
->addSelect('SUM(CASE WHEN p.position = 1 THEN 1 ELSE 0 END) AS sumwins')
->groupBy('j.id')
->orderBy('sumpoints', 'DESC')
->innerJoin('j.disputeds','d')
->addSelect('COUNT(d.id) AS nbDisputeds')
->addGroupBy('d.id');
At this point, everything works except this :
->addSelect('COUNT(d.id) AS nbDisputeds')
The data displayed is wrong probably because of my groupBy.
It should give me the number of "disputeds" occurences I have for each "j" but the result isn't correct.
Any help is welcome :)
Edit2: I tried from another repository but I get the same result, the problem is simply reported on other attributes :
$qb->select('d')
->innerJoin('d.joueur', 'j')
->addSelect('j.nom, j.prenom')
->addSelect('SUM(d.points) AS sumpoints')
->addSelect('COUNT(d) as sumparties')
->addSelect('SUM(CASE WHEN d.position = 1 THEN 1 ELSE 0 END) AS sumwins')
->GroupBy('d.joueur')
->leftJoin('j.playeds','p')
->addSelect('SUM(p.max) AS summax')
->addGroupBy('j.id')
->addGroupBy('p.partie');
In this case, I get correct values for sumpoints, sumparties, sumwins but summax gives me an incoherent value. That probably comes from a "bad" groupBy somewhere, i don't know..
Edit3 :
Works :
SELECT SUM(d0_.points) AS points, d0_.position AS position, j1_.nom AS nom, j1_.prenom AS prenom FROM disputed d0_ INNER JOIN joueur j1_ ON d0_.joueur_id = j1_.id GROUP BY d0_.joueur_id
But I don't have "max" value. Table "played" also is not joined
Doesn't Work :
SELECT SUM(d0_.points) AS points, j1_.nom AS nom, j1_.prenom AS prenom, SUM(p2_.max) AS maxs FROM disputed d0_ INNER JOIN joueur j1_ ON d0_.joueur_id = j1_.id INNER JOIN played p2_ ON j1_.id = p2_.joueur_id GROUP BY p2_.joueur_id
Table "played" is joined, "points" and "maxs" are not correct values
Database schema :
Entities :
Joueur Entity
<?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\JoueurRepository")
*/
class Joueur
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=100)
*/
private $nom;
/**
* #ORM\Column(type="string", length=100)
*/
private $prenom;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Disputed", mappedBy="joueur", orphanRemoval=true)
*/
private $disputeds;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Played", mappedBy="joueur")
*/
private $playeds;
public function __construct()
{
$this->disputeds = new ArrayCollection();
$this->playeds = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
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;
}
/**
* #return Collection|Disputed[]
*/
public function getDisputeds(): Collection
{
return $this->disputeds;
}
public function addDisputed(Disputed $disputed): self
{
if (!$this->disputeds->contains($disputed)) {
$this->disputeds[] = $disputed;
$disputed->setJoueur($this);
}
return $this;
}
public function removeDisputed(Disputed $disputed): self
{
if ($this->disputeds->contains($disputed)) {
$this->disputeds->removeElement($disputed);
// set the owning side to null (unless already changed)
if ($disputed->getJoueur() === $this) {
$disputed->setJoueur(null);
}
}
return $this;
}
public function __toString()
{
$string = $this->getPrenom().' '.$this->getNom();
return $string;
}
/**
* #return Collection|Played[]
*/
public function getPlayeds(): Collection
{
return $this->playeds;
}
public function addPlayed(Played $played): self
{
if (!$this->playeds->contains($played)) {
$this->playeds[] = $played;
$played->setJoueur($this);
}
return $this;
}
public function removePlayed(Played $played): self
{
if ($this->playeds->contains($played)) {
$this->playeds->removeElement($played);
// set the owning side to null (unless already changed)
if ($played->getJoueur() === $this) {
$played->setJoueur(null);
}
}
return $this;
}
}
Disputed Entity
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\DisputedRepository")
*/
class Disputed
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="integer")
*/
private $points;
/**
* #ORM\Column(type="integer")
*/
private $position;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Joueur", inversedBy="disputeds")
* #ORM\JoinColumn(nullable=false)
*/
private $joueur;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Tournoi", inversedBy="disputeds")
* #ORM\JoinColumn(nullable=false)
*/
private $tournament;
public function getId(): ?int
{
return $this->id;
}
public function getPoints(): ?int
{
return $this->points;
}
public function setPoints(int $points): self
{
$this->points = $points;
return $this;
}
public function getPosition(): ?int
{
return $this->position;
}
public function setPosition(int $position): self
{
$this->position = $position;
return $this;
}
public function getJoueur(): ?Joueur
{
return $this->joueur;
}
public function setJoueur(?Joueur $joueur): self
{
$this->joueur = $joueur;
return $this;
}
public function getTournament(): ?Tournoi
{
return $this->tournament;
}
public function setTournament(?Tournoi $tournament): self
{
$this->tournament = $tournament;
return $this;
}
}
Tournoi Entity
<?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\TournoiRepository")
*/
class Tournoi
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="date")
*/
private $date;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Disputed", mappedBy="tournament")
* #ORM\OrderBy({"points" = "DESC"})
*/
private $disputeds;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Partie", mappedBy="tournoi")
*/
private $Parties;
/**
* #ORM\Column(type="integer", nullable=true)
*/
private $nbPlayers;
public function __construct()
{
$this->disputeds = new ArrayCollection();
$this->parties = new ArrayCollection();
$this->Parties = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getDate(): ?\DateTimeInterface
{
return $this->date;
}
public function setDate(\DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
/**
* #return Collection|Disputed[]
*/
public function getDisputeds(): Collection
{
return $this->disputeds;
}
public function addDisputed(Disputed $disputed): self
{
if (!$this->disputeds->contains($disputed)) {
$this->disputeds[] = $disputed;
$disputed->setTournament($this);
}
return $this;
}
public function removeDisputed(Disputed $disputed): self
{
if ($this->disputeds->contains($disputed)) {
$this->disputeds->removeElement($disputed);
// set the owning side to null (unless already changed)
if ($disputed->getTournament() === $this) {
$disputed->setTournament(null);
}
}
return $this;
}
/**
* #return Collection|Partie[]
*/
public function getParties(): Collection
{
return $this->parties;
}
public function addPartie(Partie $partie): self
{
if (!$this->parties->contains($partie)) {
$this->parties[] = $partie;
$partie->setTournoi($this);
}
return $this;
}
public function removePartie(Partie $partie): self
{
if ($this->parties->contains($partie)) {
$this->parties->removeElement($partie);
// set the owning side to null (unless already changed)
if ($partie->getTournoi() === $this) {
$partie->setTournoi(null);
}
}
return $this;
}
public function getNbPlayers(): ?int
{
return $this->nbPlayers;
}
public function setNbPlayers(?int $nbPlayers): self
{
$this->nbPlayers = $nbPlayers;
return $this;
}
public function addParty(Partie $party): self
{
if (!$this->Parties->contains($party)) {
$this->Parties[] = $party;
$party->setTournoi($this);
}
return $this;
}
public function removeParty(Partie $party): self
{
if ($this->Parties->contains($party)) {
$this->Parties->removeElement($party);
// set the owning side to null (unless already changed)
if ($party->getTournoi() === $this) {
$party->setTournoi(null);
}
}
return $this;
}
}
I'm getting an
undefined index: order
error even though from what I can tell using this documentation I'm doing everything right. Any ideas?
OrdersRepository
public function findByDateAndEmployee(\DateTime $date, int $employeeId)
{
return $this->createQueryBuilder('o')
->select('o')
->join('o.employees' ,'e')
->where('e.id = :id')
->setParameter('id',$employeeId)
->getQuery()->getSql();
}
Orders Entity
class Orders
{
...
/**
* One Order has Many Employees.
* #ORM\OneToMany(targetEntity="Employee", mappedBy="order", cascade={"persist", "remove"}, orphanRemoval=TRUE)
*/
private $employees;
I know if I change the variable in "mappedBy" it will change the error message to whatever the variable name is, however when you see below "order" should be the mappedBy variable (at least, I think).
OrderEmployees Entity
class OrderEmployees
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="integer")
*/
private $orderId;
/**
* #ORM\Column(type="smallint")
*/
private $employeeId;
/**
* #ORM\ManyToOne(targetEntity="Orders", inversedBy="employees")
* #ORM\JoinColumn(name="order_id", referencedColumnName="id")
*/
private $order;
public function getId(): ?int
{
return $this->id;
}
public function getOrderId(): ?int
{
return $this->orderId;
}
public function setOrderId(int $orderId): self
{
$this->orderId = $orderId;
return $this;
}
public function getEmployeeId(): ?int
{
return $this->employeeId;
}
public function setEmployeeId(int $employeeId): self
{
$this->employeeId = $employeeId;
return $this;
}
public function getOrder(): Orders
{
return $this->order;
}
public function setOrder(Orders $order): self
{
$this->order = $order;
return $this;
}
}
Changing targetEntity="Employee" to targetEntity="OrderEmployees" in your Orders entity should help.
From the docs:
targetEntity: FQCN of the referenced target entity. Can be the
unqualified class name if both classes are in the same namespace.