[英]Symfony/Doctrine ManyToMany in the order the are assigned
I have an entity called Game
which has a ManyToMany
connection with a JoinTable
to an entity called Question
我有一个名为Game
的实体,它有一个与一个名为Question
的实体的JoinTable
的ManyToMany
连接
This works pretty well. 这非常有效。 The problem is, that I need the questions in the exact order as they are chosen, not sorted by question id, as I get them now when I call getQuestions()
on the Game
class. 问题是,我在选择时需要按照确切的顺序排列问题,而不是按问题ID排序,因为我现在在Game
类上调用getQuestions()
时会得到它们。 Is there a way to do that? 有没有办法做到这一点?
The Questions are all added with $game->addQuestion($question);
这些问题都添加了$game->addQuestion($question);
. 。 The Questions are existing, the game is persisted, after the questions are added. 问题存在,游戏在问题添加后保持不变。
...
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 {
...
}
...
you're going to have to add an intermediary entity with a sort order column. 您将不得不添加具有排序顺序列的中间实体。 Let's call it GameQuestion. 我们称之为GameQuestion。
/**
* @ORM\Table(name="game_question")
* @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
*/
class GameQuestion {
private $game;
private $question;
/**
* @Gedmo\SortablePosition
*/
private $sortOrder;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.