Doctrine Symfony ManyToMany example

Doctrine Symfony ManyToMany example

Introduction

In this post, I will show you how to use the ManyToMany relationship in Symfony with Doctrine. I will use my own code from 1payment.tools SaaS project.

Classes


NOTE: You should use the bin/console make:entity command to generate the classes.


Tool class:

src/Entity/Tool.php
<?php

namespace App\Entity;

use App\Repository\ToolRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection as DoctrineCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;

#[ORM\Entity(repositoryClass: ToolRepository::class)]
class Tool
{
    #[ORM\Id]
    #[ORM\Column(type: UuidType::NAME, unique: true)]
    #[ORM\GeneratedValue(strategy: 'CUSTOM')]
    #[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
    private ?string $id = null;

    #[ORM\Column(length: 255)]
    private ?string $title = null;

    #[ORM\ManyToMany(targetEntity: Collection::class, mappedBy: 'tool')]
    private DoctrineCollection $collections;

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

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

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): static
    {
        $this->title = $title;

        return $this;
    }

    /**
     * @return DoctrineCollection<int, Collection>
     */
    public function getCollections(): DoctrineCollection
    {
        return $this->collections;
    }

    public function addCollection(Collection $collection): static
    {
        if (!$this->collections->contains($collection)) {
            $this->collections->add($collection);
            $collection->addTool($this);
        }

        return $this;
    }

    public function removeCollection(Collection $collection): static
    {
        if ($this->collections->removeElement($collection)) {
            $collection->removeTool($this);
        }

        return $this;
    }
}

Collection class:

src/Entity/Collection.php
<?php

namespace App\Entity;

use App\Repository\CollectionsRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection as DoctrineCollection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: CollectionsRepository::class)]
class Collection
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $title = null;

    #[ORM\ManyToMany(targetEntity: Tool::class, inversedBy: 'collections')]
    private DoctrineCollection $tool;

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

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

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): static
    {
        $this->title = $title;

        return $this;
    }

    /**
     * @return DoctrineCollection<int, Tool>
     */
    public function getTool(): DoctrineCollection
    {
        return $this->tool;
    }

    public function addTool(Tool $tool): static
    {
        if (!$this->tool->contains($tool)) {
            $this->tool->add($tool);
        }

        return $this;
    }

    public function removeTool(Tool $tool): static
    {
        $this->tool->removeElement($tool);

        return $this;
    }
}


QueryBuilder example

An example of doctrine query builder with join many to many in Symfony framework:


    $this->toolRepository->createQueryBuilder('t')
            ->join('t.collections', 'c')
            ->getQuery()
            ->getResult();