简体   繁体   中英

Symfony2: ManyToMany mapping withing one Entity > SQL Error

Preface: I am developing an online quiz system. I have an entity "Quiz". The entity can be either a compilation quiz or not. If it is a compilation quiz, then it consists of other non-compilation quizes which are linked to it. A non-compilation quiz can be linked to more than one compilation quiz, so the relational mapping is ManyToMany inside the entity "Quiz".

Extract from Entity\\Quiz.php:

/**
 * @var boolean
 *
 * @ORM\Column(name="compilation", type="boolean")
 * @Ser\Expose
 */
private $compilation;

/**
 * @var Quiz[]
 *
 * @ORM\ManyToMany(targetEntity="CriticalReading\QuizBundle\Entity\Quiz", inversedBy="compiledQuiz")
 */
private $subQuizes;

/**
 * @var Quiz
 *
 * @ORM\ManyToMany(targetEntity="CriticalReading\QuizBundle\Entity\Quiz", mappedBy="subQuizes")
 */
private $compiledQuiz;

The schema validation shows no errors, but the linking table that was automatically created by the doctrine (quiz_quiz) was created with only one column "quiz_id". Additionally, when trying to link a subQuiz to a compilation quiz using $compilationQuiz->addSubQuiz($subQuiz); Symfony2 returns the error:

An exception occurred while executing 'INSERT INTO quiz_quiz (quiz_id, quiz_id) VALUES (?, ?)' with params [1, null]: SQLSTATE[42000]: Syntax error or access violation: 1110 Column 'quiz_id' specified twice

As the entire handling of the linking table and creation of linking records is done entirely automatically by the Symfony2 doctrine, I do not know what to do in order to correct this problem.

Any help/suggestions would be greatly appreciated.

Try specify column names explicity:

/**
 * @ORM\ManyToMany(targetEntity="CriticalReading\QuizBundle\Entity\Quiz", inversedBy="compiledQuiz")
 * @ORM\JoinTable(name="quiz_quiz",
 *     joinColumns={@ORM\JoinColumn(name="compiled_quiz_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="sub_quiz_id", referencedColumnName="id")}
 * )
 */
private $subQuizes;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM