DQL Jsor/doctrine-postgis - symfony

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']
],
];
}
}

Related

problem with a request on 2 tables with symfony

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.

Semantical Error line 0, col 97 near 'prix WHERE prix': Error: Class App\Entity\Produit has no association named prix

I try to join two entities with Symfony QueryBuilder. I've created a function findAllQuery in order to filter a research, but I get the error that my entity doesn't have any association. I saw already some similar questions but I can't resolve the problem.
Thanks for help !
Publication entity
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=PublicationRepository::class)
*/
class Publication
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="date", nullable=true)
*/
private $datePublication;
/**
* #ORM\ManyToOne(targetEntity=User::class, inversedBy="publications")
*/
private $user;
/**
* #ORM\ManyToOne(targetEntity=Produit::class, inversedBy="publications")
*/
private $produit;
public function getId(): ?int
{
return $this->id;
}
public function getDatePublication(): ?\DateTimeInterface
{
return $this->datePublication;
}
public function setDatePublication(?\DateTimeInterface $datePublication): self
{
$this->datePublication = $datePublication;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getProduit(): ?Produit
{
return $this->produit;
}
public function setProduit(?Produit $produit): self
{
$this->produit = $produit;
return $this;
}
}
Produit Entity
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=ProduitRepository::class)
*/
class Produit
{
public function hydrate(array $init)
{
foreach ($init as $key => $value) {
$method = "set" . ucfirst($key);
if (method_exists($this, $method)) {
$this->$method($value);
}
}
}
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $nom;
/**
* #ORM\Column(type="string", length=255)
*/
private $description;
/**
* #ORM\Column(type="boolean", nullable=true)
*/
private $vendu;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $photo;
/**
* #ORM\ManyToOne(targetEntity=Commande::class, inversedBy="produits")
*/
private $commande;
/**
* #ORM\ManyToOne(targetEntity=Type::class, inversedBy="produit")
*/
private $type;
/**
* #ORM\OneToOne(targetEntity=Publication::class, mappedBy="produit", cascade={"persist", "remove"})
*/
private $publication;
/**
* #ORM\Column(type="float")
*/
private $prix;
/**
* #ORM\OneToMany(targetEntity=Publication::class, mappedBy="produit")
*/
private $publications;
public function __construct()
{
$this->publications = 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 getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getVendu(): ?bool
{
return $this->vendu;
}
public function setVendu(?bool $vendu): self
{
$this->vendu = $vendu;
return $this;
}
public function getPhoto(): ?string
{
return $this->photo;
}
public function setPhoto(?string $photo): self
{
$this->photo = $photo;
return $this;
}
public function getCommande(): ?Commande
{
return $this->commande;
}
public function setCommande(?Commande $commande): self
{
$this->commande = $commande;
return $this;
}
public function getType(): ?Type
{
return $this->type;
}
public function setType(?Type $type): self
{
$this->type = $type;
return $this;
}
public function getPrix(): ?float
{
return $this->prix;
}
public function setPrix(float $prix): self
{
$this->prix = $prix;
return $this;
}
/**
* #return Collection|Publication[]
*/
public function getPublications(): Collection
{
return $this->publications;
}
public function addPublication(Publication $publication): self
{
if (!$this->publications->contains($publication)) {
$this->publications[] = $publication;
$publication->setProduit($this);
}
return $this;
}
public function removePublication(Publication $publication): self
{
if ($this->publications->removeElement($publication)) {
// set the owning side to null (unless already changed)
if ($publication->getProduit() === $this) {
$publication->setProduit(null);
}
}
return $this;
}
}
Publication Repository
<?php
namespace App\Repository;
use App\Entity\Publication;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use App\Entity\ProduitSearch;
use App\Entity\Produit;
/**
* #method Publication|null find($id, $lockMode = null, $lockVersion = null)
* #method Publication|null findOneBy(array $criteria, array $orderBy = null)
* #method Publication[] findAll()
* #method Publication[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class PublicationRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Publication::class);
}
public function findAllQuery (ProduitSearch $search){
if ($search->getMaxPrix()){
return $this
->createQueryBuilder('publication')
->select('produit')
->from('App\Entity\Produit','produit')
->join('produit.prix','prix')
->andWhere('prix <= :maxPrix')
->setParameter('maxPrix', $search->getMaxPrix())
->getQuery()
->getResult();
}
return $this->findAll();
}
Your query is not right. You can only join on relations and not on fields. I guess you try to get publications where the price of any produit is lower than maxPrix.
return $this
->createQueryBuilder('publication')
->join('publication.produit','produit')
->andWhere('produit.prix <= :maxPrix')
->setParameter('maxPrix', $search->getMaxPrix())
->getQuery()
->getResult();

Doctrine throwing error connected with field not related to current operation

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.

Doctrine QueryBuilder COUNT and Voters

I have some entities, for example Device entity with a voter allowing the current user to access or not some Devices.
When searching for Devices, in order to filter, I user an array_filter function which is working well.
But, I want to make some stats on my Device entity, for example the number of Devices by Brand.
My Query is OK :
$query = $this->createQueryBuilder('d')
->select('COUNT(d.id), b.name')
->join('d.model', 'm')
->join('m.Brand', 'b')
->groupBy('b.name')
;
return $query->getQuery()->getResult();
I have an array with my datas.
But the voter doesn't apply.
If I switch the user to one who may not have access to all Devices, I still see the same numbers.
So, how can I filter my COUNT request with the voters ?
The Device Entity :
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #ORM\Entity(repositoryClass="App\Repository\DeviceRepository")
*/
class Device
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=15)
*/
private $reference;
/**
* #ORM\Column(type="string", length=20)
*/
private $imei;
/**
* #ORM\Column(type="date", nullable=true)
*/
private $buyDate;
/**
* #ORM\Column(type="float", nullable=true)
*/
private $buyPrice;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\DeviceGrade")
*/
private $grade;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\CustomerGroup")
* #ORM\JoinColumn(nullable=false)
*/
private $customerGroup;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\CustomerEntity")
*/
private $customerEntity;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\CustomerSite")
*/
private $customerSite;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Model")
* #ORM\JoinColumn(nullable=false)
*/
private $model;
/**
* #ORM\Column(type="date", nullable=true)
*/
private $sellDate;
/**
* #ORM\Column(type="float", nullable=true)
*/
private $sellPrice;
/**
* #ORM\Column(type="datetime", nullable=true)
* #Gedmo\Timestampable(on="create")
*/
private $dateAdd;
/**
* #ORM\Column(type="datetime", nullable=true)
* #Gedmo\Timestampable(on="update")
*/
private $dateUpd;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User")
* #Gedmo\Blameable(on="create")
*/
private $createdBy;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User")
* #Gedmo\Blameable(on="update")
*/
private $modifiedBy;
/**
* #ORM\OneToMany(targetEntity="App\Entity\DeviceStatusHistory", mappedBy="device")
*/
private $deviceStatusHistories;
public function __construct()
{
$this->deviceStatusHistories = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(string $reference): self
{
$this->reference = $reference;
return $this;
}
public function getImei(): ?string
{
return $this->imei;
}
public function setImei(string $imei): self
{
$this->imei = $imei;
return $this;
}
public function getBuyDate(): ?\DateTimeInterface
{
return $this->buyDate;
}
public function setBuyDate(?\DateTimeInterface $buyDate): self
{
$this->buyDate = $buyDate;
return $this;
}
public function getBuyPrice(): ?float
{
return $this->buyPrice;
}
public function setBuyPrice(?float $buyPrice): self
{
$this->buyPrice = $buyPrice;
return $this;
}
public function getGrade(): ?DeviceGrade
{
return $this->grade;
}
public function setGrade(?DeviceGrade $grade): self
{
$this->grade = $grade;
return $this;
}
public function getCustomerGroup(): ?CustomerGroup
{
return $this->customerGroup;
}
public function setCustomerGroup(?CustomerGroup $customerGroup): self
{
$this->customerGroup = $customerGroup;
return $this;
}
public function getCustomerEntity(): ?CustomerEntity
{
return $this->customerEntity;
}
public function setCustomerEntity(?CustomerEntity $customerEntity): self
{
$this->customerEntity = $customerEntity;
return $this;
}
public function getCustomerSite(): ?CustomerSite
{
return $this->customerSite;
}
public function setCustomerSite(?CustomerSite $customerSite): self
{
$this->customerSite = $customerSite;
return $this;
}
public function getModel(): ?Model
{
return $this->model;
}
public function setModel(?Model $model): self
{
$this->model = $model;
return $this;
}
public function getSellDate(): ?\DateTimeInterface
{
return $this->sellDate;
}
public function setSellDate(?\DateTimeInterface $sellDate): self
{
$this->sellDate = $sellDate;
return $this;
}
public function getSellPrice(): ?float
{
return $this->sellPrice;
}
public function setSellPrice(?float $sellPrice): self
{
$this->sellPrice = $sellPrice;
return $this;
}
public function getDateAdd(): ?\DateTimeInterface
{
return $this->dateAdd;
}
public function setDateAdd(\DateTimeInterface $dateAdd): self
{
$this->dateAdd = $dateAdd;
return $this;
}
public function getDateUpd(): ?\DateTimeInterface
{
return $this->dateUpd;
}
public function setDateUpd(\DateTimeInterface $dateUpd): self
{
$this->dateUpd = $dateUpd;
return $this;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getModifiedBy(): ?User
{
return $this->modifiedBy;
}
public function setModifiedBy(?User $modifiedBy): self
{
$this->modifiedBy = $modifiedBy;
return $this;
}
/**
* #return Collection|DeviceStatusHistory[]
*/
public function getDeviceStatusHistories(): Collection
{
return $this->deviceStatusHistories;
}
public function addDeviceStatusHistory(DeviceStatusHistory $deviceStatusHistory): self
{
if (!$this->deviceStatusHistories->contains($deviceStatusHistory)) {
$this->deviceStatusHistories[] = $deviceStatusHistory;
$deviceStatusHistory->setDevice($this);
}
return $this;
}
public function removeDeviceStatusHistory(DeviceStatusHistory $deviceStatusHistory): self
{
if ($this->deviceStatusHistories->contains($deviceStatusHistory)) {
$this->deviceStatusHistories->removeElement($deviceStatusHistory);
// set the owning side to null (unless already changed)
if ($deviceStatusHistory->getDevice() === $this) {
$deviceStatusHistory->setDevice(null);
}
}
return $this;
}
public function __toString(): string
{
return $this->reference.' / '.$this->imei.' / '.$this->getModel()->getName().' - '.
$this->getModel()->getBrand()->getName().' - '.$this->getModel()->getColor()->getName().
' - '.$this->getModel()->getStorage()->getCapacity();
}
}
The Model Entity :
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File;
/**
* #ORM\Entity(repositoryClass="App\Repository\ModelRepository")
* #Vich\Uploadable
*/
class Model
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\Column(type="boolean")
*/
private $isActive;
/**
* #ORM\Column(type="date", nullable=true)
*/
private $dateStartSell;
/**
* #ORM\Column(type="date", nullable=true)
*/
private $dateEndSell;
/**
* #ORM\Column(type="date", nullable=true)
*/
private $dateEndSupport;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Brand", inversedBy="models")
* #ORM\JoinColumn(nullable=false)
*/
private $Brand;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Color")
* #ORM\JoinColumn(nullable=false)
*/
private $color;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Storage")
*/
private $storage;
/**
* #ORM\Column(type="datetime", nullable=true)
* #Gedmo\Timestampable(on="create")
*/
private $dateAdd;
/**
* #ORM\Column(type="datetime", nullable=true)
* #Gedmo\Timestampable(on="update")
*/
private $dateUpd;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User")
* #Gedmo\Blameable(on="create")
*/
private $createdBy;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User")
* #Gedmo\Blameable(on="update")
*/
private $modifiedBy;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\CustomerGroup", inversedBy="models")
*/
private $customerGroup;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* #Vich\UploadableField(mapping="model_image", fileNameProperty="imageName", size="imageSize")
*
* #var File
*/
private $imageFile;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*
* #var string
*/
private $imageName;
/**
* #ORM\Column(type="integer", nullable=true)
*
* #var integer
*/
private $imageSize;
public function __construct(?File $imageFile = null)
{
$this->customerGroup = new ArrayCollection();
$this->imageFile = $imageFile;
if (null !== $imageFile) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->dateUpd = new \DateTimeImmutable();
}
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getDateStartSell(): ?\DateTimeInterface
{
return $this->dateStartSell;
}
public function setDateStartSell(?\DateTimeInterface $dateStartSell): self
{
$this->dateStartSell = $dateStartSell;
return $this;
}
public function getDateEndSell(): ?\DateTimeInterface
{
return $this->dateEndSell;
}
public function setDateEndSell(?\DateTimeInterface $dateEndSell): self
{
$this->dateEndSell = $dateEndSell;
return $this;
}
public function getDateEndSupport(): ?\DateTimeInterface
{
return $this->dateEndSupport;
}
public function setDateEndSupport(?\DateTimeInterface $dateEndSupport): self
{
$this->dateEndSupport = $dateEndSupport;
return $this;
}
public function getBrand(): ?Brand
{
return $this->Brand;
}
public function setBrand(?Brand $Brand): self
{
$this->Brand = $Brand;
return $this;
}
public function getColor(): ?Color
{
return $this->color;
}
public function setColor(?Color $color): self
{
$this->color = $color;
return $this;
}
public function getStorage(): ?Storage
{
return $this->storage;
}
public function setStorage(?Storage $storage): self
{
$this->storage = $storage;
return $this;
}
public function getDateAdd(): ?\DateTimeInterface
{
return $this->dateAdd;
}
public function setDateAdd(\DateTimeInterface $dateAdd): self
{
$this->dateAdd = $dateAdd;
return $this;
}
public function getDateUpd(): ?\DateTimeInterface
{
return $this->dateUpd;
}
public function setDateUpd(\DateTimeInterface $dateUpd): self
{
$this->dateUpd = $dateUpd;
return $this;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getModifiedBy(): ?User
{
return $this->modifiedBy;
}
public function setModifiedBy(?User $modifiedBy): self
{
$this->modifiedBy = $modifiedBy;
return $this;
}
public function __toString(): string
{
return $this->getBrand()->getName().' '.$this->getName().' '.$this->getColor()->getName().' '.$this->getStorage()->getCapacity();
}
/**
* #return Collection|CustomerGroup[]
*/
public function getCustomerGroup(): Collection
{
return $this->customerGroup;
}
public function addCustomerGroup(CustomerGroup $customerGroup): self
{
if (!$this->customerGroup->contains($customerGroup)) {
$this->customerGroup[] = $customerGroup;
}
return $this;
}
public function removeCustomerGroup(CustomerGroup $customerGroup): self
{
if ($this->customerGroup->contains($customerGroup)) {
$this->customerGroup->removeElement($customerGroup);
}
return $this;
}
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* #param File|\Symfony\Component\HttpFoundation\File\UploadedFile $imageFile
*/
public function setImageFile(?File $imageFile = null): void
{
$this->imageFile = $imageFile;
if (null !== $imageFile) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->dateUpd = new \DateTimeImmutable();
}
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageName(?string $imageName): void
{
$this->imageName = $imageName;
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function setImageSize(?int $imageSize): void
{
$this->imageSize = $imageSize;
}
public function getImageSize(): ?int
{
return $this->imageSize;
}
}
And the Brand Entity :
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #ORM\Entity(repositoryClass="App\Repository\BrandRepository")
* #ORM\HasLifecycleCallbacks()
*/
class Brand
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\Column(type="datetime")
* #Gedmo\Timestampable(on="create")
*/
private $dateAdd;
/**
* #ORM\Column(type="datetime")
* #Gedmo\Timestampable(on="create")
*/
private $dateUpd;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User")
* #ORM\JoinColumn(nullable=false)
* #Gedmo\Blameable(on="create")
*/
private $createdBy;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User")
* #ORM\JoinColumn(nullable=false)
* #Gedmo\Blameable(on="update")
*/
private $modifiedBy;
/**
* #ORM\Column(type="boolean")
*/
private $isDeleted;
/**
* #ORM\Column(type="boolean")
*/
private $isActive;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Model", mappedBy="Brand")
*/
private $models;
public function __construct()
{
$this->models = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDateAdd(): ?\DateTimeInterface
{
return $this->dateAdd;
}
public function setDateAdd(\DateTimeInterface $dateAdd): self
{
$this->dateAdd = $dateAdd;
return $this;
}
public function getDateUpd(): ?\DateTimeInterface
{
return $this->dateUpd;
}
public function setDateUpd(\DateTimeInterface $dateUpd): self
{
$this->dateUpd = $dateUpd;
return $this;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getModifiedBy(): ?User
{
return $this->modifiedBy;
}
public function setModifiedBy(?User $modifiedBy): self
{
$this->modifiedBy = $modifiedBy;
return $this;
}
public function getIsDeleted(): ?bool
{
return $this->isDeleted;
}
public function setIsDeleted(bool $isDeleted): self
{
$this->isDeleted = $isDeleted;
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
/**
* #return Collection|Model[]
*/
public function getModels(): Collection
{
return $this->models;
}
public function addModel(Model $model): self
{
if (!$this->models->contains($model)) {
$this->models[] = $model;
$model->setBrand($this);
}
return $this;
}
public function removeModel(Model $model): self
{
if ($this->models->contains($model)) {
$this->models->removeElement($model);
// set the owning side to null (unless already changed)
if ($model->getBrand() === $this) {
$model->setBrand(null);
}
}
return $this;
}
}
Thanks for your help !
Best,
Julien
The voter won't update your query. It is used to check if a user is authorized to access some part of your code.
If you want to have your Devices according to your Voter, you'll need to write the correct query based on you need !
Thanks for your messages
#Alexandre : I'm going to search this way.
What I've done to achieve my goal :
- Add a bidirectionnal relation with inversedby in model and brand entities.
This way, I can make that :
$brands = $this->getDoctrine()->getRepository(Brand::class)->findAll();
$arrayBrands = array();
$arrayDevicesCount = array();
foreach($brands as $brand)
{
$devicesCount = 0;
$models = $brand->getModels();
foreach ($models as $model)
{
$devices = $model->getDevices()->getValues();
$devices = array_filter($devices, function (Device $device){
return $this->isGranted('view', $device);
});
$devicesCount+= count($devices);
}
if($devicesCount > 0)
{
array_push($arrayBrands, $brand->getName());
array_push($arrayDevicesCount, $devicesCount);
}
}
And this is working well !

Get data from join

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

Resources