I'm trying to display data of each match in a table like this one:
img1 | name1 | score1 | score2 | name2 | img2 | date | statut_id
Here is how I would do it in sql:
SELECT j1.image AS img1, j1.name AS name1, s1.score AS score1, s2.score AS score2, j2.name AS name2, j2.image AS img2, m.date AS date, m.statut_id FROM jouster j1, jouster j2, matchs m, score s1, score s2 WHERE j1.id = s1.jouster_id AND m.id = s1.matchs_id AND j2.id = s2.jouster_id AND m.id = s2.matchs_id AND m.jouster1_id = j1.id AND m.jouster2_id = j2.id
I want to do it with the "symfony way" but i'm not used to it
I have an issue with scores. When I try to access to the score of each jousters I get this error:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string").
match.html.twig
{% for match in matchs %}
{{match.jouster1.image}}
{{match.jouster1.name}}
{{match.jouster1.scores}}
-
{{match.jouster2.scores}}
{{match.jouster2.name}}
{{match.jouster2.image}}
{{match.date | date('Y-m-d H:i:s') }}
<br>
{% endfor %}
MatchController.php
/**
* @Route("/match", name="match")
*/
public function match() {
$matchs = $this->getDoctrine()->getRepository(Matchs::class)->findAll();
return $this->render('page/match.html.twig', [
'matchs' => $matchs
]);
}
Matchs.php
class Matchs {
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="datetime")
*/
private $date;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Jouster", inversedBy="jouster2")
* @ORM\JoinColumn(nullable=false)
*/
private $jouster1;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Jouster", inversedBy="matchAsJouster2")
* @ORM\JoinColumn(nullable=false)
*/
private $jouster2;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Score", mappedBy="matchs", orphanRemoval=true)
*/
private $scores;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Statut", inversedBy="matchs")
* @ORM\JoinColumn(nullable=false)
*/
private $statut;
*************************
*** getters & setters ***
*************************
/**
* @return Collection|Score[]
*/
public function getScores(): Collection
{
return $this->scores;
}
public function addScore(Score $score): self
{
if (!$this->scores->contains($score)) {
$this->scores[] = $score;
$score->setMatchs($this);
}
return $this;
}
Score.php
class Score
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="smallint", nullable=true)
*/
private $score;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Jouster", inversedBy="scores")
* @ORM\JoinColumn(nullable=false)
*/
private $jouster;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Matchs", inversedBy="scores")
* @ORM\JoinColumn(nullable=false)
*/
private $matchs;
public function getId(): ?int
{
return $this->id;
}
public function getScore(): ?int
{
return $this->score;
}
public function setScore(int $score): self
{
$this->score = $score;
return $this;
}
public function getJouster(): ?Jouster
{
return $this->jouster;
}
public function setJouster(?Jouster $jouster): self
{
$this->jouster = $jouster;
return $this;
}
public function getMatchs(): ?Matchs
{
return $this->matchs;
}
public function setMatchs(?Matchs $matchs): self
{
$this->matchs = $matchs;
return $this;
}
}
Jouster.php
/**
* @return Collection|Score[]
*/
public function getScores(): Collection
{
return $this->scores;
}
public function addScore(Score $score): self
{
if (!$this->scores->contains($score)) {
$this->scores[] = $score;
$score->setJouster($this);
}
return $this;
}
public function removeScore(Score $score): self
{
if ($this->scores->contains($score)) {
$this->scores->removeElement($score);
// set the owning side to null (unless already changed)
if ($score->getJouster() === $this) {
$score->setJouster(null);
}
}
return $this;
}
EDIT: I had to do a loop in order to display matchs scores.
{% for match in matchs %}
{{match.jouster1.image}}
{{match.jouster1.name}}
{% for score in match.scores%}
{{score.score}}
{% endfor %}
{{match.jouster2.name}}
{{match.jouster2.image}}
{{match.date | date('Y-m-d H:i:s') }}
as DarkBee stated in he's comment.
The error is clearly explained in the error message itself:
PersistentCollection could not be converted to string
Meaning, you had twig try to output something, which was actually not 1 thing, but a collection of things. (Scores in your case)