Ako implementovať vzťah 'has many through' v Doctrine?
sobes.tech AI
Odpoveď od AI
V Doctrine2, vzťah "has many through" sa realizuje prostredníctvom jasného vytvorenia prepojovacej entity (join entity), ktorá spája dve ďalšie entity s vzťahmi Many-to-One.
-
Vytvoria sa tri triedy entít: Zdrojová, Cieľová a Prepojovacia (JoinEntity).
-
Prepojovacia entita (napríklad,
UserGrouppre "User has many Group through UserGroup") obsahuje dve polia, ktoré sú vzťahmi Many-to-One k Zdrojovej (User) a Cieľovej (Group) entitám. -
Zdrojové a Cieľové entity majú vzťah One-to-Many k Prepojovacej entite.
-
Pre jednoduchší prístup k Cieľovým entitám cez Zdrojovú (napríklad, získať všetky
GrouppreUser), môžete pridať metódu do Zdrojovej entity, ktorá bude filtrovať Prepojovacie entity.
Príklad štruktúry:
// User.php
<?php
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\OneToMany(targetEntity: UserGroup::class, mappedBy: 'user')]
private Collection $userGroups; // Vzťah k prepojovacej entite
public function __construct()
{
$this->userGroups = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* Získanie skupín používateľa cez prepojovaciu entitu.
* Toto je ekvivalent "has many through".
* Poznámka: Môžete chcieť načítať túto kolekciu efektívne v závislosti od vašich potrieb.
*/
public function getGroups(): Collection
{
return $this->userGroups->map(fn(UserGroup $userGroup) => $userGroup->getGroup());
}
public function getUserGroups(): Collection
{
return $this->userGroups;
}
public function addUserGroup(UserGroup $userGroup): static
{
if (!$this->userGroups->contains($userGroup)) {
$this->userGroups->add($userGroup);
$userGroup->setUser($this);
}
return $this;
}
public function removeUserGroup(UserGroup $userGroup): static
{
if ($this->userGroups->removeElement($userGroup)) {
// nastaviť vlastnú stranu na null (ak ešte nie je zmenené)
if ($userGroup->getUser() === $this) {
$userGroup->setUser(null);
}
}
return $this;
}
}
// Group.php
<?php
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Group
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\OneToMany(targetEntity: UserGroup::class, mappedBy: 'group')]
private Collection $userGroups; // Vzťah k prepojovacej entite
public function __construct()
{
$this->userGroups = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUserGroups(): Collection
{
return $this->userGroups;
}
public function addUserGroup(UserGroup $userGroup): static
{
if (!$this->userGroups->contains($userGroup)) {
$this->userGroups->add($userGroup);
$userGroup->setGroup($this);
}
return $this;
}
public function removeUserGroup(UserGroup $userGroup): static
{
if ($this->userGroups->removeElement($userGroup)) {
// nastaviť vlastnú stranu na null (ak ešte nie je zmenené)
if ($userGroup->getGroup() === $this) {
$userGroup->setGroup(null);
}
}
return $this;
}
}
// UserGroup.php
<?php
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: 'user_user_group')] // Príklad názvu tabuľky
class UserGroup
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'userGroups')]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null; // Many-to-One k User
#[ORM\ManyToOne(targetEntity: Group::class, inversedBy: 'userGroups')]
#[ORM\JoinColumn(nullable: false)]
private ?Group $group = null; // Many-to-One k Group
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
public function getGroup(): ?Group
{
return $this->group;
}
public function setGroup(?Group $group): static
{
$this->group = $group;
return $this;
}
}
Tento prístup, aj keď si vyžaduje vytvorenie ďalšej entity, poskytuje úplnú kontrolu nad údajmi o väzbách a umožňuje ľahko pridávať ďalšie polia do prepojovacej tabuľky (napríklad dátum pripojenia používateľa ku skupine).