Ascherer
Ascherer

Reputation: 8083

Doctrine creating useless join table

so, i have two entities: Genre and Game

Genre.php

<?php

namespace Acme\Bundle\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Genre
 *
 * @ORM\Table(name="genre")
 * @ORM\Entity
 */
class Genre
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=64, nullable=false)
     */
    protected $name;

    /**
     * @var string
     *
     * @ORM\Column(name="display", type="string", length=64, nullable=false)
     */
    protected $display;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text", nullable=false)
     */
    protected $description;

    /**
     * @var ArrayCollection|Game[]
     *
     * @ORM\ManyToMany(targetEntity="Game", inversedBy="genres", cascade={"persist"})
     */
    protected $games;

    // ... Irrelevant Constructor and following getters/setters
}

Game.php

<?php

namespace Acme\Bundle\DemoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Game
 *
 * @ORM\Table(name="game")
 * @ORM\Entity
 */
class Game
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=256, nullable=false)
     */
    protected $name;

    /**
     * @var string
     *
     * @ORM\Column(name="display", type="string", length=256, nullable=false)
     */
    protected $display;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text", nullable=false)
     */
    protected $description;

    /**
     * @var ArrayCollection|Genre[]
     *
     * @ORM\ManyToMany(targetEntity="Genre", inversedBy="games", cascade={"persist"})
     * @ORM\JoinTable(name="genre_game",
     *    joinColumns={@ORM\JoinColumn(name="genre_id", referencedColumnName="id")},
     *    inverseJoinColumns={@ORM\JoinColumn(name="game_id", referencedColumnName="id")}
     *    )
     */
    protected $genres;

    /**
     * @var ArrayCollection|Platform[]
     *
     * @ORM\ManyToMany(targetEntity="Platform", inversedBy="games", cascade={"persist"})
     * @ORM\JoinTable(name="platform_game",
     *    joinColumns={@ORM\JoinColumn(name="platform_id", referencedColumnName="id")},
     *    inverseJoinColumns={@ORM\JoinColumn(name="game_id", referencedColumnName="id")}
     *    )
     */
    protected $platforms;

    /**
     * @var Image[]
     *
     * @ORM\OneToMany(targetEntity="Image",mappedBy="game_id", cascade={"persist"})
     */
    protected $images;
}

When i run php app/console doctrine:schema:create or update, it creates all of the needed join tables I specified above, but it also creates genre_genre

This table is always empty, and doesnt seem to do anything, and prevents me from running php app/console doctrine:schema:update's later, and its trying to add an index to it that already exists

Anyone see what I'm doing wrong?

Upvotes: 2

Views: 215

Answers (2)

Jessica
Jessica

Reputation: 7005

Game->genres and Genre-game are inversed by each other, which is invalid - one needs to be owning. I believe there is a doctine:schema:validate command you would find useful.

Upvotes: 1

Momin
Momin

Reputation: 868

Sir, I think you have made many to many relation(bidirectional) in those mapping classes. According to doctrine documentation that will create that table for relation of those Game and Genre like in the example.

You can create the db table then create the mapper class to verify with generate-entities. This way you can verify the schema and the mapping.

Upvotes: 0

Related Questions