Sobes.tech
Middle+

Doctrine-də 'has many through' münasibətini necə həyata keçirmək olar?

sobes.tech Süni İntellekt

AI-dan cavab

Doctrine2-də "has many through" münasibəti açıq şəkildə bir keçid entitası (join entity) yaradaraq həyata keçirilir, bu da iki digər entiti ilə Many-to-One münasibətləri ilə əlaqələndirilir.

  1. Üç entiti sinfi yaradılır: Mənbə, Hədəf və Aralıq (JoinEntity).

  2. Aralıq entitası (məsələn, UserGroup "User many Group through UserGroup" üçün) iki sahəni əhatə edir, bunlar Many-to-One münasibətləri ilə Mənbə (User) və Hədəf (Group) entiti ilə əlaqələndirilir.

  3. Mənbə və Hədəf entiti Aralıq entitiyə One-to-Many münasibətinə malikdir.

  4. Asanlıqla Hədəf entitiylərinə Mənbə vasitəsilə daxil olmaq üçün (məsələn, User üçün bütün Group-ları əldə etmək), Mənbə entitiyinə metod əlavə etmək mümkündür, bu da Aralıq entitiyləri filtrləyəcək.

Struktur nümunəsi:

// 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; // Aralıq entitasiyə münasibət

    public function __construct()
    {
        $this->userGroups = new ArrayCollection();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * İstifadəçinin qruplarını aralıq entitasiyə vasitəsilə əldə et.
     * Bu "has many through"-a bərabərdir.
     * Qeyd: Bu kolleksiyanı ehtiyacınıza uyğun səmərəli yükləmək istəyə bilərsiniz.
     */
    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; // Aralıq entitasiyə münasibət

    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')] // Nümunə cədvəl adı
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 User-ə

    #[ORM\ManyToOne(targetEntity: Group::class, inversedBy: 'userGroups')]
    #[ORM\JoinColumn(nullable: false)]
    private ?Group $group = null; // Many-to-One 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 yanaşma, əlavə entiti yaratmağı tələb etsə də, tam nəzarəti təmin edir və aralıq cədvələ əlavə sahələr (məsələn, istifadəçinin qrupa qoşulma tarixi) əlavə etməyə imkan verir.