Moopsish
Moopsish

Reputation: 164

How to join multiple tables in symfony

I'm trying to link 3 tables to each other in symfony.

I'm trying to link movies to multiple genre's using 3 tables.

tables: films, movietogenre, genres

I'm trying to link a film id to a movietogenre id to a genre. this is what I have:

class films:

       /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         * @var ArrayCollection
         *
         * @ORM\ManyToOne(targetEntity="movietogenre", inversedBy="films")
         * @ORM\JoinColumn(name="movie_id", referencedColumnName="movie_id")
         */
        private $id;

class movietogenre

  /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="movie_id", type="integer")
     *
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="films", mappedBy="movietogenre")
     */
    private $movieId;

    /**
     * @var integer
     *
     * @ORM\Column(name="genre_id", type="integer")
     *
     *@var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="genres", mappedBy="movietogenre")
     */
    private $genreId;

class genres:

  /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @var ArrayCollection
     *
     * @ORM\ManyToOne(targetEntity="movietogenre", inversedBy="genres")
     * @ORM\JoinColumn(name="genre_id", referencedColumnName="id")
     */
    private $id;

twig:

    {% for entity in entities if entity.einddatum|date('Y-m-d') > "now"|date("Y-m-d") %}
    <div class="movie-item col-md-12 col-lg-12 col-sm-12 col-xs-12">
        <div class="poster col-md-2 col-lg-2 col-sm-2 col-xs-0">
            <img  class="img-responsive" src="{{ entity.filmposter }}" />
        </div>
        <div class="info col-md-10 col-lg-10 col-sm-10 col-xs-12">
            <a href="{{ path('films_show', { 'id': entity.id }) }}"><h2>{{ entity.titel }}</h2></a>
            <p>{{ entity.omschrijving }}</p>
            <p class="text-muted">{{ entity.id.genre_id.genre }} | speelduur: {{ entity.speelduur|date("H:i:s") }} </p>
            <a href="{{ path('films_show', { 'id': entity.id }) }}"> Meer informatie </a>
        </div>
    </div>
    {% endfor %}

I know this is probably really bad so please correct me.

thanks ahead,

Upvotes: 1

Views: 102

Answers (1)

Grzegorz Gajda
Grzegorz Gajda

Reputation: 2474

Class Movie

class Movie {
    /**
     * @ManyToMany(targetEntity="Genre", inversedBy="movies")
     * @JoinTable(name="movietogenre")
     **/
    private $genres;

    public function __construct() {
        $this->genres = new \Doctrine\Common\Collections\ArrayCollection();
}

Class Genre:

class Genre {    
    /**
     * @ManyToMany(targetEntity="Movie", mappedBy="genres")
    **/
    private $movies;

    public function __construct() {
        $this->movies = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

This is valid construction for ManyToMany relation. Read more about it here: Doctrine 2 ORM documentation - 5. Association Mapping - 5.9. Many-To-Many, Bidirectional.

Upvotes: 1

Related Questions