Jak zaimplementować relację 'has many through' w Doctrine?
sobes.tech AI
Odpowiedź od AI
W Doctrine2 relacja "has many through" jest realizowana poprzez jawne utworzenie encji pośredniej (join entity), która łączy dwie inne encje relacjami Many-to-One.
-
Tworzone są trzy klasy encji: Źródłowa, Docelowa i Pośrednia (JoinEntity).
-
Encja pośrednia (np.
UserGroupdla "User ma wiele Group przez UserGroup") zawiera dwa pola, które są relacjami Many-to-One z encjami Źródłową (User) i Docelową (Group). -
Encje Źródłowa i Docelowa mają relację One-to-Many z encją pośrednią.
-
Aby ułatwić dostęp do encji Docelowych przez encję Źródłową (np. pobranie wszystkich
GroupdlaUser), można dodać metodę w encji Źródłowej, która będzie filtrować encje pośrednie.
Przykład struktury:
// 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; // Relacja do encji pośredniej
public function __construct()
{
$this->userGroups = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* Pobierz grupy użytkownika przez encję pośrednią.
* To jest równoważne z "has many through".
* Uwaga: możesz chcieć pobrać tę kolekcję efektywnie w zależności od potrzeb.
*/
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)) {
// ustaw stronę właściciela na null (chyba że już zmieniono)
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; // Relacja do encji pośredniej
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)) {
// ustaw stronę właściciela na null (chyba że już zmieniono)
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')] // Przykład nazwy tabeli
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 do User
#[ORM\ManyToOne(targetEntity: Group::class, inversedBy: 'userGroups')]
#[ORM\JoinColumn(nullable: false)]
private ?Group $group = null; // Many-to-One do 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;
}
}
Ten sposób, choć wymaga utworzenia dodatkowej encji, zapewnia pełną kontrolę nad danymi relacji i umożliwia łatwe dodawanie dodatkowych pól do tabeli pośredniej (np. data dołączenia użytkownika do grupy).