Symfony/Doctrine ManyToMany 按照分配的顺序

Symfony/Doctrine ManyToMany in the order the are assigned

我有一个名为 Game 的实体,它与一个名为 Question

的实体有 ManyToMany 连接 JoinTable

这很有效。问题是,我需要按照选择的确切顺序排列问题,而不是按问题 ID 排序,因为我现在在 Game class 上调用 getQuestions() 时得到它们。 有办法吗?

题目全部加$game->addQuestion($question);。问题存在,游戏持续存在,添加问题后。

...
class Game {
    ...
    /**
     * @ORM\ManyToMany(targetEntity="Question")
     * @ORM\JoinTable(name="Games_to_Questions",
     *      joinColumns={@ORM\JoinColumn(name="game_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="question_id", referencedColumnName="id")}
     *      )
     **/
    private $questions;
    ...
}
...
class Question {
    ...
}
...

您将不得不添加一个带有排序顺序列的中间实体。我们称它为 GameQuestion。

/**
 * @ORM\Table(name="game_question")
 * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
 */
class GameQuestion {
    private $game;
    private $question;
    /**
     * @Gedmo\SortablePosition
     */
    private $sortOrder;
}