对教条的@OneToMany ArrayCollection进行排序

Ala*_*blo 5 php one-to-many symfony doctrine-orm

我的问题接近这个问题,但并不完全适合我的问题.

我在一个实体中有这个专栏:

/**
 * @var ArrayCollection[SubjectTag]
 *
 * @ORM\OneToMany(targetEntity="SubjectTag", mappedBy="subject")
 * @Assert\Count(max = 10, maxMessage = "You can't create more than 10 tags.")
 * @Assert\Valid()
 */
protected $subjectTags;
Run Code Online (Sandbox Code Playgroud)

我想通过定义的位置动态地对我的标签进行排序SubjectTag.position.

Mat*_*teo 6

尝试使用doctrine2 ORM功能进行Ordering To-Many Associations,如下所示:

/**
 * @var ArrayCollection[SubjectTag]
 *
 * @ORM\OneToMany(targetEntity="SubjectTag", mappedBy="subject")
 * @ORM\OrderBy({"position" = "ASC"})
 * @Assert\Count(max = 10, maxMessage = "You can't create more than 10 tags.")
 * @Assert\Valid()
 */
protected $subjectTags;
Run Code Online (Sandbox Code Playgroud)

希望这有帮助