N
N
Nikita Gushchin2014-06-13 21:38:12
symfony
Nikita Gushchin, 2014-06-13 21:38:12

Implementing a Many-to-Many Relationship?

Good afternoon! I recently started learning symfony2 and stumbled upon the following problem:
there are several entities (for simplicity, let's say that they have only one property - the name) - a movie, an employee and a profession. I need to create a many-to-many relationship - "each movie has a staff (several staff members), and each staff member has a specific job for that movie." For different films, the same employee may have different professions. On pure mysql, I would make an additional table with three columns: movie_id | employe_id | profession_id . But I don't know what to do with symfony\doctrine.
Thanks in advance
Essence Film:

/**
 * @ORM\Entity
 * @ORM\Table(name="movie")
 */
class Movie
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $name;
}

Essence worker:
/**
 * @ORM\Entity
 * @ORM\Table(name="employe")
 */
class Employe
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string",length=100)
     */
    protected $name;
}

The essence of the profession:
/**
 * @ORM\Entity
 * @ORM\Table(name="Profession")
 */
class Profession
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string",length=100)
     */
    protected $name;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-06-14
@iNikNik

Pretty popular problem .
Usually solved by introducing an intermediate entity - for example, EmployeProfession. In this entity, I will only have connections with Employe and Profession, a kind of proxy entity. To simplify the design, you can already use getters inside the entity, disable lazy loading if necessary, etc. There are also other options, but they are not universal.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question