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.
Related
I'm triying to get a calculated value from database where this value depends on an external value. I got the next function:
public function findAllBy($search, $order, $limit, $offset) {
$qb = $this->entityManager
->createQueryBuilder()
->select('a, (ST_DISTANCE(ST_MakePoint(0,0), ST_MakePoint(0,0)) / 1000) as distance')
->from('App\Entity\App\Advertisement', 'a')
->setMaxResult($limit)
->setFirstResult($offset);
for ($order as $key => $value)
$qb->andWhere($key !== "distance" ? "a.{$key}" : "distance", $value);
return $qb->getQuery()->getResult();
}
This function works, but the problem comes now. Like I said, I need to modify two parameters, one of theme come from outside of this function, that's not a problem, the problem comes with the other one. I the advertisement table I got a column named "coordinate" of type Point (postgis), when I try to substitute one of ST_MakePoint like this:
(ST_DISTANCE(ST_MakePoint(a.coordinate[0] a.coordinate[1]), ST_MakePoint(0,0)) / 1000) as distance
I got the next error:
"[Syntax Error] line 0, col 49: Error: Expected Doctrine\\ORM\\Query\\Lexer::T_COMMA, got '['"
I also tried:
(ST_DISTANCE(a.coordinate::geometry, ST_MakePoint(0,0)) / 1000) as distance
"[Syntax Error] line 0, col 35: Error: Expected Doctrine\\ORM\\Query\\Lexer::T_COMMA, got ':'"
Other:
ERROR: function st_distance(point, geometry) does not exist
(ST_DISTANCE(a.coordinate, ST_MakePoint(0,0)) / 1000) as distance // Error coordinate not a geometry
Other:
[Syntax Error] line 0, col 54: Error: Expected Doctrine\\ORM\\Query\\Lexer::T_CLOSE_PARENTHESIS, got ':'"
(ST_DISTANCE(a.coordinate, ST_MakePoint(0,0)::point) / 1000) as distance
None of them works. I dont how can I use the coordinate column on DQL.
If that helps, I got another function that works, but it is SQL not DQL, and I dont know how to parse the result as entity. Here it is:
SELECT x.id
FROM (
SELECT
g.id,
(ST_DISTANCE(ST_MakePoint(g.coordinate[0], g.coordinate[1])::geography, ST_MakePoint($point)::geography) / 1000) as distance
FROM app.graveyard g
) AS x
WHERE x.distance <= 100
ORDER BY x.distance ASC
Entity:
<?php
namespace App\Entity\App;
use App\DBAL\Types\Geolocation\Point;
use App\Repository\App\AdvertisementRepository;
use App\Tools\Tools;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\MaxDepth;
/**
* #ORM\Entity(repositoryClass=AdvertisementRepository::class)
* #ORM\Table(schema="app")
*/
class Advertisement
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity=App\Entity\App\Company::class, inversedBy="advertisements")
* #ORM\JoinColumn(nullable=false)
*/
private $company;
/**
* #ORM\Column(type="AdvertisementTypeEnum")
*/
private $type;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\Column(type="string", length=500)
*/
private $description;
/**
* #ORM\Column(type="float")
*/
private $price;
/**
* #ORM\Column(type="integer")
*/
private $nicheTime;
/**
* #ORM\Column(type="boolean")
*/
private $enabled;
/**
* #ORM\OneToMany(targetEntity=App\Entity\App\AdvertisementComplement::class, mappedBy="advertisement")
*/
private $advertisementComplements;
/**
* #ORM\OneToMany(targetEntity=App\Entity\App\AdvertisementTransfer::class, mappedBy="advertisement")
*/
private $advertisementTransfers;
/**
* #ORM\OneToMany(targetEntity=App\Entity\App\AdvertisementGraveyard::class, mappedBy="advertisement")
*/
private $advertisementGraveyards;
/**
* #ORM\OneToMany(targetEntity=App\Entity\App\AdvertisementFuneralParlor::class, mappedBy="advertisement")
*/
private $advertisementFuneralParlors;
/**
* #ORM\OneToMany(targetEntity=AdvertisementCrematorium::class, mappedBy="advertisement")
*/
private $advertisementCrematoria;
/**
* #ORM\Column(type="integer")
*/
private $query;
/**
* #ORM\Column(type="datetime")
*/
private $createAt;
/**
* #ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* #ORM\OneToMany(targetEntity=AdvertisementStatistic::class, mappedBy="advertisement")
*/
private $advertisementStatistics;
/**
* #ORM\Column(type="date", nullable=true)
*/
private $publishDate;
/**
* #ORM\OneToMany(targetEntity=AdvertisementRate::class, mappedBy="advertisement")
*/
private $advertisementRates;
/**
* #ORM\Column(type="integer", options={"default":10000})
*/
private $version;
/**
* #ORM\OneToMany(targetEntity=Contract::class, mappedBy="advertisement", cascade={"persist"}, fetch="LAZY")
*/
private $contracts;
/**
* #ORM\Column(type="boolean")
*/
private $wake;
/**
* #ORM\Column(type="integer", nullable=true)
*/
private $wakeTime;
/**
* #ORM\Column(type="float", nullable=true)
*/
private $wakePrice;
/**
* #ORM\Column(type="float", nullable=true)
*/
private $kmExtraPrice;
/**
* #ORM\Column(type="boolean", nullable=true)
*/
private $wakeIncluded;
/**
* #ORM\Column(type="datetime_immutable", nullable=true)
*/
private $deletedAt;
/**
* #ORM\ManyToOne(targetEntity=City::class)
*/
private $city;
/**
* #ORM\Column(type="point", nullable=true)
*/
private $coordinate;
public function __construct()
{
$this->advertisementComplements = new ArrayCollection();
$this->advertisementTransfers = new ArrayCollection();
$this->advertisementGraveyards = new ArrayCollection();
$this->advertisementFuneralParlors = new ArrayCollection();
$this->advertisementCrematoria = new ArrayCollection();
$this->createAt = new \DateTime("now");
$this->updatedAt = new \DateTime("now");
$this->advertisementStatistics = new ArrayCollection();
$this->advertisementRates = new ArrayCollection();
$this->contracts = new ArrayCollection();
$this->coordinate = $this->city->getCoordinate();
}
public function getId(): ?int
{
return $this->id;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getType()
{
return $this->type;
}
public function setType($type): self
{
$this->type = $type;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
public function getNicheTime(): ?int
{
return $this->nicheTime;
}
public function setNicheTime(?int $nicheTime): self
{
$this->nicheTime = $nicheTime;
return $this;
}
public function getEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
/**
* #return Collection|AdvertisementComplement[]|null
*/
public function getAdvertisementComplements(): ?Collection
{
return $this->advertisementComplements;
}
public function addAdvertisementComplement(AdvertisementComplement $advertisementComplement): self
{
if (!$this->advertisementComplements->contains($advertisementComplement)) {
$this->advertisementComplements[] = $advertisementComplement;
$advertisementComplement->setAdvertisement($this);
}
return $this;
}
public function removeAdvertisementComplement(AdvertisementComplement $advertisementComplement): self
{
if ($this->advertisementComplements->removeElement($advertisementComplement)) {
// set the owning side to null (unless already changed)
if ($advertisementComplement->getAdvertisement() === $this) {
$advertisementComplement->setAdvertisement(null);
}
}
return $this;
}
/**
* #return Collection|AdvertisementTransfer[]|null
*/
public function getAdvertisementTransfers(): ?Collection
{
return $this->advertisementTransfers;
}
public function addAdvertisementTransfer(AdvertisementTransfer $advertisementTransfer): self
{
if (!$this->advertisementTransfers->contains($advertisementTransfer)) {
$this->advertisementTransfers[] = $advertisementTransfer;
$advertisementTransfer->setAdvertisement($this);
}
return $this;
}
public function removeAdvertisementTransfer(AdvertisementTransfer $advertisementTransfer): self
{
if ($this->advertisementTransfers->removeElement($advertisementTransfer)) {
// set the owning side to null (unless already changed)
if ($advertisementTransfer->getAdvertisement() === $this) {
$advertisementTransfer->setAdvertisement(null);
}
}
return $this;
}
/**
* #return Collection|AdvertisementGraveyard[]
*/
public function getAdvertisementGraveyards(): Collection
{
return $this->advertisementGraveyards;
}
public function addAdvertisementGraveyard(AdvertisementGraveyard $advertisementGraveyard): self
{
if (!$this->advertisementGraveyards->contains($advertisementGraveyard)) {
$this->advertisementGraveyards[] = $advertisementGraveyard;
$advertisementGraveyard->setAdvertisement($this);
}
return $this;
}
public function removeAdvertisementGraveyard(AdvertisementGraveyard $advertisementGraveyard): self
{
if ($this->advertisementGraveyards->removeElement($advertisementGraveyard)) {
// set the owning side to null (unless already changed)
if ($advertisementGraveyard->getAdvertisement() === $this) {
$advertisementGraveyard->setAdvertisement(null);
}
}
return $this;
}
/**
* #return Collection|AdvertisementFuneralParlor[]
*/
public function getAdvertisementFuneralParlors(): Collection
{
return $this->advertisementFuneralParlors;
}
public function addAdvertisementFuneralParlor(AdvertisementFuneralParlor $advertisementFuneralParlor): self
{
if (!$this->advertisementFuneralParlors->contains($advertisementFuneralParlor)) {
$this->advertisementFuneralParlors[] = $advertisementFuneralParlor;
$advertisementFuneralParlor->setAdvertisement($this);
}
return $this;
}
public function removeAdvertisementFuneralParlor(AdvertisementFuneralParlor $advertisementFuneralParlor): self
{
if ($this->advertisementFuneralParlors->removeElement($advertisementFuneralParlor)) {
// set the owning side to null (unless already changed)
if ($advertisementFuneralParlor->getAdvertisement() === $this) {
$advertisementFuneralParlor->setAdvertisement(null);
}
}
return $this;
}
public function getQuery(): ?int
{
return $this->query;
}
public function setQuery(int $query): self
{
$this->query = $query;
return $this;
}
/**
* #return Collection|AdvertisementCrematorium[]
*/
public function getAdvertisementCrematoria(): Collection
{
return $this->advertisementCrematoria;
}
public function addAdvertisementCrematorium(AdvertisementCrematorium $advertisementCrematorium): self
{
if (!$this->advertisementCrematoria->contains($advertisementCrematorium)) {
$this->advertisementCrematoria[] = $advertisementCrematorium;
$advertisementCrematorium->setAdvertisement($this);
}
return $this;
}
public function removeAdvertisementCrematorium(AdvertisementCrematorium $advertisementCrematorium): self
{
if ($this->advertisementCrematoria->removeElement($advertisementCrematorium)) {
// set the owning side to null (unless already changed)
if ($advertisementCrematorium->getAdvertisement() === $this) {
$advertisementCrematorium->setAdvertisement(null);
}
}
return $this;
}
public function getCreateAt(): ?\DateTimeInterface
{
return $this->createAt;
}
public function getCreateAtNormalized(): string
{
return $this->createAt->format("Y-m-d H:i:s");
}
public function setCreateAt(): self
{
$this->createAt = new \DateTime("now");
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function getUpdateAtNormalized(): string
{
return $this->updatedAt->format("Y-m-d H:i:s");
}
public function setUpdatedAt(): self
{
$this->updatedAt = new \DateTime("now");
return $this;
}
/**
* #return Collection|AdvertisementStatistic[]
*/
public function getAdvertisementStatistics(): Collection
{
return $this->advertisementStatistics;
}
public function addAdvertisementStatistic(AdvertisementStatistic $advertisementStatistic): self
{
if (!$this->advertisementStatistics->contains($advertisementStatistic)) {
$this->advertisementStatistics[] = $advertisementStatistic;
$advertisementStatistic->setAdvertisement($this);
}
return $this;
}
public function removeAdvertisementStatistic(AdvertisementStatistic $advertisementStatistic): self
{
if ($this->advertisementStatistics->removeElement($advertisementStatistic)) {
// set the owning side to null (unless already changed)
if ($advertisementStatistic->getAdvertisement() === $this) {
$advertisementStatistic->setAdvertisement(null);
}
}
return $this;
}
public function getPublishDate(): ?\DateTimeInterface
{
return $this->publishDate;
}
public function setPublishDate(): self
{
$this->publishDate = new \DateTime("now");
return $this;
}
public function clearPublishDate(): self
{
$this->publishDate = null;
return $this;
}
/**
* #return Collection|AdvertisementRate[]
*/
public function getAdvertisementRates(): Collection
{
return $this->advertisementRates;
}
public function addAdvertisementRate(AdvertisementRate $advertisementRate): self
{
if (!$this->advertisementRates->contains($advertisementRate)) {
$this->advertisementRates[] = $advertisementRate;
$advertisementRate->setAdvertisement($this);
}
return $this;
}
public function removeAdvertisementRate(AdvertisementRate $advertisementRate): self
{
if ($this->advertisementRates->removeElement($advertisementRate)) {
// set the owning side to null (unless already changed)
if ($advertisementRate->getAdvertisement() === $this) {
$advertisementRate->setAdvertisement(null);
}
}
return $this;
}
public function getVersion(): int
{
return $this->version;
}
public function getStringVersion(): string
{
$v = $this->getVersion();
$result = "";
while ($v > 0) {
$result = ((strlen($result) + 2) % 3) == 0 ? '.' . $v %10 . $result : $v %10 . $result;
$v = (int)($v / 10);
}
return $result;
}
public function setVersion(int $version): self
{
$this->version = $version;
return $this;
}
/**
* #return Collection|Contract[]
*/
public function getContracts(): Collection
{
return $this->contracts;
}
public function addContract(Contract $contract): self
{
if (!$this->contracts->contains($contract)) {
$this->contracts[] = $contract;
$contract->setAdvertisement($this);
}
return $this;
}
public function removeContract(Contract $contract): self
{
if ($this->contracts->removeElement($contract)) {
// set the owning side to null (unless already changed)
if ($contract->getAdvertisement() === $this) {
$contract->setAdvertisement(null);
}
}
return $this;
}
public function getWake(): bool
{
return $this->wake;
}
public function setWake(bool $wake): self
{
$this->wake = $wake;
return $this;
}
public function getWakeTime(): ?int
{
return $this->wakeTime;
}
public function setWakeTime(?int $wakeTime): self
{
$this->wakeTime = $wakeTime;
return $this;
}
public function getWakePrice(): ?float
{
return $this->wakePrice;
}
public function setWakePrice(?float $wakePrice): self
{
$this->wakePrice = $wakePrice;
return $this;
}
public function getKmExtraPrice(): ?float
{
return $this->kmExtraPrice;
}
public function setKmExtraPrice(?float $kmExtraPrice): self
{
$this->kmExtraPrice = $kmExtraPrice;
return $this;
}
public function getWakeIncluded(): ?bool
{
return $this->wakeIncluded;
}
private function getJsonRates(): array
{
$size = sizeof($this->advertisementRates);
if ($size == 0) return [];
$average = 0.0;
$jsonRates = [];
foreach ($this->advertisementRates as $advertisementRate) {
array_push($jsonRates, $advertisementRate->toJsonArray());
$average += $advertisementRate->getScore();
}
$average /= $size;
return ["average" => $average, "rates" => $jsonRates, "size" => $size];
}
public function getDeletedAt(): ?\DateTimeImmutable
{
return $this->deletedAt;
}
public function setDeletedAt(?\DateTimeImmutable $deletedAt): self
{
$this->deletedAt = $deletedAt;
return $this;
}
public function setWakeIncluded(?bool $wakeIncluded): self
{
$this->wakeIncluded = $wakeIncluded;
return $this;
}
public function getCity(): ?City
{
return $this->city;
}
public function setCity(?City $city): self
{
$this->city = $city;
return $this;
}
public function getDistance(Point $destiny)
{
dump($this->tools);
return 0;
// return $this->tools->getDistanceBetweenPoint($this->coordinate, $destiny);
}
public function toJsonArray(): array
{
$publishDate = $this->getPublishDate();
$rates = $this->getJsonRates();
return [
"id" => $this->id,
"companyId" => $this->company->getId(),
"name" => $this->name,
"description" => $this->description,
"type" => $this->type,
"basePrice" => $this->price,
"query" => $this->query,
"updateAt" => $this->getUpdateAtNormalized(),
"publishDate" => !is_null($publishDate) ? $publishDate->format("d-m-Y") : null,
"enable" => $this->enabled,
"version" => $this->getStringVersion(),
"wake" => $this->wake,
"wakeIncluded" => $this->wakeIncluded,
"wakePrice" => $this->wakePrice,
"wakeTime" => $this->wakeTime,
"kmExtraPrice" => $this->kmExtraPrice,
"rates" => empty($rates) ?
null :
[
"average" => $rates['average'],
"rates" => $rates['rates'],
"ratesCount" => $rates['size']
],
];
}
}
I trie to get the documents stored with a message in the message_document table with a doctrine request but the request loops and ends up filling my memory
I tried the same request with sql on Dbeaver and it runs with no problem
great thanks for your help
My message.php
enter code here<?php
namespace App\Entity;
use DateTime;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\MessageRepository;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity(repositoryClass=MessageRepository::class)
*/
class Message
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="text")
*/
private $content;
/**
* #ORM\Column(type="datetime")
*/
private $createdAt;
/**
* #ORM\ManyToOne(targetEntity=Conversation::class, inversedBy="messages")
*/
private $conversation;
/**
* #ORM\ManyToOne(targetEntity=User::class, inversedBy="messages")
*/
private $user;
/**
* #ORM\OneToMany(targetEntity=MessageDocument::class, mappedBy="messages")
*/
private $messageDocuments;
public function __construct( string $content, User $user, Conversation $converstation) {
$this->content = $content;
$this->user = $user;
$this->conversation = $converstation;
$this->createdAt = new \DateTime('now');
$this->messageDocuments = new ArrayCollection();
}
public function getId():?int {
return $this->id;
}
public function getContent():?string {
return $this->content;
}
public function setContent(string $content):self {
$this->content = $content;
return $this;
}
public function getCreatedAt():?\DateTimeInterface {
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt):self {
$this->createdAt = $createdAt;
return $this;
}
public function getConversation():?Conversation
{
return $this->conversation;
}
public function setConversation(?Conversation $conversation):self {
$this->conversation = $conversation;
return $this;
}
public function getUser():?User {
return $this->user;
}
public function setUser(?User $user):self {
$this->user = $user;
return $this;
}
/**
* #return Collection|MessageDocument[]
*/
public function getMessageDocuments(): Collection
{
return $this->messageDocuments;
}
public function addMessageDocument(MessageDocument $messageDocument): self
{
if (!$this->messageDocuments->contains($messageDocument)) {
$this->messageDocuments[] = $messageDocument;
$messageDocument->setMessages($this);
}
return $this;
}
public function removeMessageDocument(MessageDocument $messageDocument): self
{
if ($this->messageDocuments->removeElement($messageDocument)) {
// set the owning side to null (unless already changed)
if ($messageDocument->getMessages() === $this) {
$messageDocument->setMessages(null);
}
}
return $this;
}
}
my MessageDocument.php
enter code here<?php
namespace App\Entity;
use App\Repository\MessageDocumentRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=MessageDocumentRepository::class)
*/
class MessageDocument
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $fileName;
/**
* #ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $type;
/**
* #ORM\ManyToOne(targetEntity=Message::class, inversedBy="messageDocuments")
*/
private $message;
/**
* #ORM\ManyToOne(targetEntity=User::class, inversedBy="messageDocuments")
*/
private $sender;
public function getId(): ?int
{
return $this->id;
}
public function getFileName(): ?string
{
return $this->fileName;
}
public function setFileName(string $fileName): self
{
$this->fileName = $fileName;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
public function getMessage(): ?Message
{
return $this->message;
}
public function setMessage(?Message $message): self
{
$this->message = $message;
return $this;
}
public function getSender(): ?User
{
return $this->sender;
}
public function setSender(?User $sender): self
{
$this->sender = $sender;
return $this;
}
}
the request on MessageDocument join Message
/**
* #return MessageDocument[] Returns an array of MessageDocument objects
*/
//$qb->expr()->eq('md.id = :val')
//,Join::WITH,$qb->expr()->eq('md.id = :val')
public function findDocByMessageId($messageId)
{
return $this->createQueryBuilder('md')
->select('md')
->join('md.message','m')
->where('m.id =:val')
->setParameter('val', $messageId)
->setMaxResults(20)
->getQuery()
->getResult();
}
the calling of the repo request
$allMessages = new ArrayCollection();
$docs=[];
foreach ($messages as $messageUnique) {
$messId = $messageUnique->getId();
$documentsMessages = $messageRepository->findDocByMessageId($messId);
if($documentsMessages !== null){
foreach($documentsMessages as $document){
$docs=$document;
}
//$messageUnique->addMessageDocument($document);
}
$conversation->setLastMessage($messageUnique);
$messageUnique = array(
'id' => $messageUnique->getId(),
'author' => $messageUnique->getUser()->getFullName(),
'authorId' => $messageUnique->getUser()->getId(),
'content' => $messageUnique->getContent(),
'createdAt' => $messageUnique->getCreatedAt()
);
$allMessages->add($messageUnique);
}
I finally solved the problem :)
I have used the #ignore decorator on two properties of the MessageDocument entity
I have changed my code and now error message is about circular reference
Message entity
<?php
namespace App\Entity;
use DateTime;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\MessageRepository;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity(repositoryClass=MessageRepository::class)
*/
class Message
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="text")
*/
private $content;
/**
* #ORM\Column(type="datetime")
*/
private $createdAt;
/**
* #ORM\ManyToOne(targetEntity=Conversation::class, inversedBy="messages")
*/
private $conversation;
/**
* #ORM\ManyToOne(targetEntity=User::class, inversedBy="messages")
*/
private $user;
/**
* #ORM\OneToMany(targetEntity=MessageDocument::class, mappedBy="message")
*/
private $messageDocument;
public function __construct( string $content, User $user, Conversation $converstation) {
$this->content = $content;
$this->user = $user;
$this->conversation = $converstation;
$this->createdAt = new \DateTime('now');
$this->messageDocuments = new ArrayCollection();
}
public function getId():?int {
return $this->id;
}
public function getContent():?string {
return $this->content;
}
public function setContent(string $content):self {
$this->content = $content;
return $this;
}
public function getCreatedAt():?\DateTimeInterface {
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt):self {
$this->createdAt = $createdAt;
return $this;
}
public function getConversation():?Conversation
{
return $this->conversation;
}
public function setConversation(?Conversation $conversation):self {
$this->conversation = $conversation;
return $this;
}
public function getUser():?User {
return $this->user;
}
public function setUser(?User $user):self {
$this->user = $user;
return $this;
}
/**
* #return Collection|MessageDocument[]
*/
public function getMessageDocument(): Collection
{
return $this->messageDocument;
}
public function addMessageDocument(MessageDocument $messageDocument): self
{
if (!$this->messageDocument->contains($messageDocument)) {
$this->messageDocument[] = $messageDocument;
$messageDocument->setMessages($this);
}
return $this;
}
public function removeMessageDocument(MessageDocument $messageDocument): self
{
if ($this->messageDocuments->removeElement($messageDocument)) {
// set the owning side to null (unless already changed)
if ($messageDocument->getMessages() === $this) {
$messageDocument->setMessages(null);
}
}
return $this;
}
}
MessageDocument entity
<?php
namespace App\Entity;
use App\Repository\MessageDocumentRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=MessageDocumentRepository::class)
*/
class MessageDocument
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $fileName;
/**
* #ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $type;
/**
* #ORM\ManyToOne(targetEntity=Message::class, inversedBy="messageDocument")
*/
private $message;
/**
* #ORM\ManyToOne(targetEntity=User::class, inversedBy="messageDocument")
*/
private $sender;
public function getId(): ?int
{
return $this->id;
}
public function getFileName(): ?string
{
return $this->fileName;
}
public function setFileName(string $fileName): self
{
$this->fileName = $fileName;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
public function getMessage(): ?Message
{
return $this->message;
}
public function setMessage(?Message $message): self
{
$this->message = $message;
return $this;
}
public function getSender(): ?User
{
return $this->sender;
}
public function setSender(?User $sender): self
{
$this->sender = $sender;
return $this;
}
}
js use to call controller from twig
function getMessages(conversationId , userId) {
superConversationId = conversationId;
userIdEnCours = userId;
//* On vide ce qu'il y avait avant
removeAllChildNodes(document.querySelector('.msg_history'));
//* On remet toutes les conversations en blanc
let allDivs = document.getElementsByClassName('chat_list');
for (let div of allDivs) {
div.style.background = '#f8f8f8';
}
//* background-color light-grey quand conversation selectionné
$("#"+ conversationId).css('background', "#e1e1e1")
let xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
//si requête validé on traite la réponse
if (this.readyState == 4 && this.status == 200) {
let messages = JSON.parse(this.response);
let discussion = document.querySelector('.msg_history');
for (let message of messages) {
let dateMessage = new Date(message.createdAt)
var lastMessageId = message.id;
//* Affichage selon envoyé ou reçu
if (message.authorId == userIdEnCours) {
discussion.innerHTML += "<div class=\"outgoing_msg col-12 \"\><div class=\"sent_msg col-6 m-0\">"
+ "<p class='m-0'>" + message.content + "</p>"
+ "<span class=\"time_date\"> De " + message.author + " | " + dateMessage.toLocaleString() + "</span>"
+ "</div>"
;
} else {
discussion.innerHTML += "<div class=\"incoming_msg col-12 \">"
+ "<div class=\"received_msg col-12\"\><div class=\"received_withd_msg col-6\">"
+ "<p>" + message.content + "</p>"
+ "<span class=\"time_date_receiver\"> De " + message.author + " | " + dateMessage.toLocaleString() + "</span>"
+ "</div></div>"
;
}
}
//* scroll dernier message
let divMessagerie = document.querySelector(".msg_history");
divMessagerie.scrollTop = divMessagerie.scrollHeight;
//vl le 13/09
// ne voyant pas l'utilité ...
/* Interval = setInterval( () => {
$.ajax({
type: "POST",
url : "/checkMessage/" + lastMessageId,
success: function (response) {
if (response === true) {
clearInterval(Interval);
getMessages(
superConversationId,
userIdEnCours
);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}, 5000); */
}
};
//! Ouverture de la requete (MOCK VERS PROD)
//* PROD
// xmlhttp.open("GET", "http://www.generation-boomerang.com/messagerie/conv/" + conversationId);
//* DEV
xmlhttp.open("GET", "/messagerie/conv/" + conversationId);
xmlhttp.send();
}
The controller to get the messages from a conversation
/**
* #Route("/messagerie/conv/{id}" , name="messagerie_getMessagesOfConv")
* #Security("is_granted('ROLE_ABONNE') or is_granted('ROLE_ADMIN')", message="Merci de vous abonner au portail pour bénéficier de cette super fonctionnalité !")
*/
public function getMessagesOfConv(int $id, EntityManagerInterface $entityManager, ConversationRepository $conversationRepository, ParticipantRepository $participantRepository,MessageDocumentRepository $messageRepository) {
//* Récup du user + check à faire
$userEncours = $this->getUser();
$userId = $userEncours->getId();
//* Ckeck si la conversation appartient bien au user
$check = $participantRepository->checkBelongs($id, $userId);
if ($check != 1) {
return $this->json('cette conv ne te regarde pas !');
}
$conversation = $conversationRepository->find($id);
$messages = $conversation->getMessages();
// $documentsMessages = new ArrayCollection();//vl
$allMessages = new ArrayCollection();
// $docs=;
foreach ($messages as $messageUnique) {
$messId = $messageUnique->getId();
$documentsMessages = $messageRepository->findDocByMessageId($messId);
if($documentsMessages !== null){
foreach($documentsMessages as $document){
// $docs=$document;
$messageUnique->addMessageDocument($document);
}
}
$conversation->setLastMessage($messageUnique);
$messageUnique = array(
'id' => $messageUnique->getId(),
'author' => $messageUnique->getUser()->getFullName(),
'authorId' => $messageUnique->getUser()->getId(),
'content' => $messageUnique->getContent(),
'createdAt' => $messageUnique->getCreatedAt(),
'messageDocuments'=>$messageUnique->getMessageDocument()
);
$allMessages->add($messageUnique);
}
$entityManager->persist($conversation);
$entityManager->flush();
//echo '<pre>'; var_dump( $conversation);exit;echo '</pre>';//
return $this->json($allMessages);
}
conversation entity
<?php
namespace App\Entity;
use App\Repository\ConversationRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=ConversationRepository::class)
*/
class Conversation {
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\OneToOne(targetEntity=Message::class, cascade={"persist", "remove"})
*/
private $lastMessage;
/**
* #ORM\OneToMany(targetEntity=Message::class, mappedBy="conversation")
*/
private $messages;
/**
* #ORM\OneToMany(targetEntity="Participant", mappedBy="conversation")
*/
private $participants;
/**
* #ORM\Column(type="string" , length=50)
*/
private $title;
/**
* #ORM\Column(type="datetime")
*/
private $createdAt;
public function __construct() {
$this->participants = new ArrayCollection();
$this->messages = new ArrayCollection();
$this->createdAt = new \DateTime('now');
}
public function getId():?int {
return $this->id;
}
public function getLastMessage():?Message {
return $this->lastMessage;
}
public function setLastMessage(?Message $lastMessage):self {
$this->lastMessage = $lastMessage;
return $this;
}
/**
* #return Collection|Message[]
*/
public function getMessages():Collection {
return $this->messages;
}
public function addMessage(Message $message):self {
if (!$this->messages->contains($message)) {
$this->messages[] = $message;
$message->setConversation($this);
}
return $this;
}
public function removeMessage(Message $message):self {
if ($this->messages->contains($message)) {
$this->messages->removeElement($message);
// set the owning side to null (unless already changed)
if ($message->getConversation() === $this) {
$message->setConversation(null);
}
}
return $this;
}
/**
* #return Collection|Participant[]
*/
public function getParticipants():Collection {
return $this->participants;
}
public function addParticipant(Participant $participant):self {
if (!$this->participants->contains($participant)) {
$this->participants[] = $participant;
$participant->setConversation($this);
}
return $this;
}
public function removeParticipant(Participant $participant):self {
if ($this->participants->contains($participant)) {
$this->participants->removeElement($participant);
// set the owning side to null (unless already changed)
if ($participant->getConversation() === $this) {
$participant->setConversation(null);
}
}
return $this;
}
/**
* Get the value of title
*/
public function getTitle() {
return $this->title;
}
/**
* Set the value of title
* #return self
*/
public function setTitle($title) {
$this->title = $title;
return $this;
}
public function getCreatedAt():?\DateTimeInterface {
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt):self {
$this->createdAt = $createdAt;
return $this;
}
}
and participant entity
<?php
namespace App\Entity;
use App\Repository\ParticipantRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=ParticipantRepository::class)
*/
class Participant
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="participants")
*/
private $user;
/**
* #ORM\ManyToOne(targetEntity="Conversation", inversedBy="participants")
*/
private $conversation;
private $messageReadAt;
/**
* Get the value of id
*/
public function getId()
{
return $this->id;
}
/**
* Get the value of user
*/
public function getUser()
{
return $this->user;
}
/**
* Set the value of user
*
* #return self
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get the value of conversation
*/
public function getConversation()
{
return $this->conversation;
}
/**
* Set the value of conversation
*
* #return self
*/
public function setConversation($conversation)
{
$this->conversation = $conversation;
return $this;
}
/**
* Get the value of messageReadAt
*/
public function getMessageReadAt()
{
return $this->messageReadAt;
}
/**
* Set the value of messageReadAt
*
* #return self
*/
public function setMessageReadAt($messageReadAt)
{
$this->messageReadAt = $messageReadAt;
return $this;
}
}
there is also a CreateMessageHandler and GetMessageHanler (but I don't understand how it works)
CreateMessageHandler
<?php
namespace App\MessageHandler;
use App\Entity\User;
use App\Entity\Message;
use App\Entity\Conversation;
use App\Message\CreateMessage;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
class CreateMessageHandler implements MessageHandlerInterface
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager) {
$this->entityManager = $entityManager;
}
public function __invoke(CreateMessage $createMessage)
{
$conversation = $this->entityManager->getRepository(Conversation::class)->find($createMessage->getConversationId());
if(is_null($conversation))
{
$conversation = new Conversation();
$this->entityManager->persist($conversation);
$this->entityManager->flush();
} else {
$message = new Message(
$createMessage->getContent(),
$this->entityManager->getRepository(User::class)->find($createMessage->getUserId()),
$conversation,
);
// if ()
}
// Debug
// echo $createMessage->getContent();
// echo $message->getUser()->getId();
// echo $message->getConversation()->getId();
$this->entityManager->persist($message);
$this->entityManager->flush();
}
}
GetMessageHandler
<?php
namespace App\MessageHandler;
use App\Entity\Message;
use App\Message\GetMessages;
use App\Repository\MessageRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
class GetMessagesHandler implements MessageHandlerInterface
{
private $entityManager;
private $messageRepository;
public function __construct(EntityManagerInterface $entityManager, MessageRepository $messageRepository) {
$this->entityManager = $entityManager;
}
public function __invoke(GetMessages $message)
{
//Récupérer les messages de la conversation
return $this->entityManager->getRepository(Message::class)->findBy(['conversation' => $message->getConversationId()]);
}
}
I think everything is there.
A little long sorry...
Hope somebody could find the reason why I have this circular reference
Thanks
I'm not sure at all about setting $messageUnique = array() with value of the same variable, you should probably use another name of variable. If you have an infinite loop problem it's coming from the PHP in your case.
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"
]
}
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've tried something like "Use-Case 3: Join-Table with Metadata".
I've used "Cart", "Item" and "CartItem" objects.
I get the items like $cart->getItems();.
Let's assume that there are 4 items. Now I delete one item like $items->remove(1); to remove an item.
$items->count() tells me that one item has been removed (it says 3). Also when look in the code, I see that the collection has changed.
So when I flush, I expect that the CartItem will be removed, nut nothing happens. Even when I add the items again like $cart->setItems($items); nothing happens at flush time.
Deleting an item should be straight forward, isn't it? What am I doing wrong?
Edit Edit Edit
Even with a "normal" key it doesn't work. I've created some new objects:
Soup
/**
* #Entity
* #Table(name="Soups")
**/
class Soup {
/**
* #Id #Column(type="integer")
* #GeneratedValue
**/
protected $id;
/**
* #Column(type="string", length=50)
*/
protected $name;
/**
* #OneToMany(targetEntity="SoupIngredient", mappedBy="soup", cascade={"persist"});
*/
protected $soupIngredients;
public function __construct() {
$this->soupIngredients = new ArrayCollection();
}
public function getId() {
// Return id.
return $this->id;
}
public function getName() {
// Return value.
return $this->name;
}
public function setName($value) {
// Set value.
$this->name = $value;
// Return.
return $this;
}
public function getSoupIngredients() {
// Return value.
return $this->soupIngredients;
}
public function setSoupIngredients($value) {
// Set value.
$this->soupIngredients = $value;
// Return.
return $this;
}
}
Ingredient
/**
* #Entity
* #Table(name="Ingredients")
**/
class Ingredient {
/**
* #Id #Column(type="integer")
* #GeneratedValue
**/
protected $id;
/**
* #Column(type="text", length=50)
*/
protected $name;
/**
* #OneToMany(targetEntity="SoupIngredient", mappedBy="ingredient", cascade={"persist"});
*/
protected $soupIngredients;
public function __construct() {
$this->soupIngredients = new ArrayCollection();
}
public function getId() {
// Return id.
return $this->id;
}
public function getName() {
// Return value.
return $this->name;
}
public function setName($value) {
// Set value.
$this->name = $value;
// Return.
return $this;
}
public function getSoupIngredients() {
// Return value.
return $this->soupIngredients;
}
public function setSoupIngredients($value) {
// Set value.
$this->soupIngredients = $value;
// Return.
return $this;
}
}
SoupIngredients
/**
* #Entity
* #Table(name="SoupIngredients")
**/
class SoupIngredient {
/**
* #Id #Column(type="integer")
* #GeneratedValue
**/
protected $id;
/**
* #Column(type="float")
*/
protected $quantity;
/**
* #ManyToOne(targetEntity="Soup", inversedBy="soupIngredients")
*/
protected $soup;
/**
* #ManyToOne(targetEntity="Ingredient", inversedBy="soupIngredients")
*/
protected $ingredient;
public function __construct() {
}
public function getId() {
// Return id.
return $this->id;
}
public function getQuantity() {
// Return value.
return $this->quantity;
}
public function setQuantity($value) {
// Set value.
$this->quantity = $value;
// Return.
return $this;
}
public function getSoup() {
// Return value.
return $this->soup;
}
public function setSoup($value) {
// Set value.
$this->soups = $value;
// Return.
return $this;
}
public function getIngredient() {
// Return value.
return $this->ingredient;
}
public function setIngredient($value) {
// Set value.
$this->ingredient = $value;
// Return.
return $this;
}
}
I get a Soup object that has 5 ingredients.
// Get Soup #1.
$em->getRepository("Soup")->find(1);
// Get ingredients.
$ingredients = $soup->getIngredients();
// Remove ingredient #2.
$ingredients->remove(2);
// Well, let's flush.
$em->flush();
Nothing happens. I can remove the SoupIngredient with the $em, but I want to use it in a function ($soup->removeIngredient($ingredient)) so I don't always have the $em available. Is the only way to achieve this to use the $em->remove($soupIngredient) (then it will be updated at flush-time).
Never mind! orphanRemoval=true works like magic...
Stupid me!