Doctrine'da 'has many through' munosabatini qanday amalga oshirish mumkin?
sobes.tech AI
AIdan javob
Doctrine2 da "has many through" munosabatini aniq bo'lak entity (join entity) yaratish orqali amalga oshiriladi, bu esa ikki boshqa entity ni Many-to-One munosabatlari bilan bog'laydi.
-
Uchta entity sinfi yaratiladi: Manba, Maqsad va Oraliq (JoinEntity).
-
Oraliq entity (masalan,
UserGroup"User has many Group through UserGroup" uchun) ikki maydonni o'z ichiga oladi, ular Manba (User) va Maqsad (Group) entity lariga Many-to-One munosabatlar bo'lib. -
Manba va Maqsad entity lar Oraliq entity ga One-to-Many munosabatga ega.
-
Maqsad entity larga Manba orqali qulay kirish uchun (masalan,
Useruchun barchaGrouplarni olish), Manba entity ga metod qo'shish mumkin, bu Oraliq entity larni filtrlash uchun.
Struktura misoli:
// 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; // Oraliq entity bilan munosabat
public function __construct()
{
$this->userGroups = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* Manba orqali foydalanuvchi guruhlarini olish.
* Bu "has many through" ga teng.
* Eslatma: Bu kolleksiyani samarali olish uchun ehtiyojingizga qarab optimallashtirish mumkin.
*/
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)) {
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; // Oraliq entity bilan munosabat
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)) {
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')] // Masalan, jadval nomi
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 ga User
#[ORM\ManyToOne(targetEntity: Group::class, inversedBy: 'userGroups')]
#[ORM\JoinColumn(nullable: false)]
private ?Group $group = null; // Many-to-One ga 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;
}
}
Bu yondashuv, qo'shimcha entity yaratishni talab qilsa ham, to'liq nazoratni ta'minlaydi va oraliq jadvalga qo'shimcha maydonlar qo'shishni osonlashtiradi (masalan, foydalanuvchi guruhiga qo'shilish sanasi).