G
G
gitdev2020-12-31 00:26:12
symfony
gitdev, 2020-12-31 00:26:12

How to select a ManyToMany field in Symfony?

Symphony 5.2

/**
 * @ORM\Entity(repositoryClass=VacancyRepository::class)
 * @ORM\Table(name="vacancies")
 */
class Vacancy
{
    /**
     * @ORM\ManyToMany(targetEntity=Skill::class, inversedBy="vacancies")
     */
    private $skill;
}

...

There is a table with skills
/**
 * @ORM\Entity(repositoryClass=SkillsRepository::class)
 * @ORM\Table(name="skills")
 */
class Skill
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

    /**
     * @ORM\ManyToMany(targetEntity=Vacancy::class, mappedBy="skill")
     */
    private $vacancies;

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


There is an intermediate table vacancy_skill

How to make a selection in Query Builder. Need to add a table that is ManyToMany(Skills) and specify the list of id that arrives to us from the front?
$vacanciesQueryBuilder = $entityManager->getConnection()
            ->createQueryBuilder()
            ->select('v.*, v.english_level')
            ->from($entityManager->getClassMetadata(Vacancy::class)->getTableName(), 'v')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question