从联接中获取数据

时间:2019-03-27 15:41:36

标签: symfony doctrine

我在这里苦了一段时间。我需要显示一些数据表。我必须从不同实体获得此数据。我无法正确获得一个值,因为我的联接似乎无法更正数据。

他是我的存储库功能:

    $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');

在这一点上,除此以外,其他所有方法都有效:

->addSelect('COUNT(d.id) AS nbDisputeds')

显示的数据有误,可能是由于我的groupBy。 它应该给我每个“ j”出现的“争议”事件的数量,但结果不正确。

欢迎任何帮助:)

Edit2:我从另一个存储库尝试过,但是得到了相同的结果,只是在其他属性上报告了该问题:

    $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');

在这种情况下,我得到的总和,sumparties,sumwins的值正确,但是summax给我的值是不连贯的。可能来自一个“坏”小组。我不知道。。

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

但是我没有“ max”值。表“已播放”也未加入

不起作用:

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

表“ played”已联接,“ points”和“ maxs”不是正确的值

数据库架构:

enter image description here

实体:

Joueur实体

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

争议实体

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

图尔诺伊实体

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

}

0 个答案:

没有答案