V
V
vvmax012021-08-13 16:09:46
symfony
vvmax01, 2021-08-13 16:09:46

How to search in a table on a ManyToMany field?

2 tables with related fields Many to Many relationship

Record table
class Records
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $title;

    /**
     * @ORM\Column(type="string", length=40)
     */
    private $link;

    /**
     * @ORM\Column(type="text")
     */
    private $description;

    /**
     * @ORM\ManyToOne(targetEntity=Versions::class, inversedBy="records")
     * @ORM\JoinColumn(nullable=false)
     */
    private $version;

    /**
     * @ORM\Column(type="boolean")
     */
    private $visible;

    /**
     * @ORM\ManyToMany(targetEntity=Mods::class, inversedBy="records")
     */
    private $mods;

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


Table Mods
class Mods
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity=Records::class, mappedBy="mods")
     */
    private $records;

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


link table created automatically
61166e74d9e2d678376751.png

A set of several Mods table ids, using them to find all records from the Records table that contain at least one of the Mods table ids.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anmanerr, 2021-09-13
@Anmanerr

There are repositories for this. You can create a repository for the Mods entity and use it to get the Mods you need and vice versa.
And it is customary to call entities in the singular, while the tables for them are in the plural.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question