Doctrine ORM with Symfony2 - 对多对多关系进行排序
我有 2 个实体。
Mag
Page
其中有ManyToMany关系。一个页面可以附加许多杂志,但我需要允许用户调整杂志的排序顺序。
目前,顺序仅基于 Page_Mag
表中 Mag 的 id
所以我需要一个排序字段,但随后我需要每个字段都有一个排序字段页面->杂志关系,因为有许多页面可以附加任意数量的杂志。
我正在努力想出最好的方法来做到这一点,而又不会把我的模式搞得一团糟!
有什么想法吗?
我的注释是:
/**
* @ORM\ManyToMany(targetEntity="Page", mappedBy="magazines" , cascade = {"persist", "remove"})
*/
protected $Page;
和
/**
* @ORM\ManyToMany(targetEntity="Magazine", inversedBy="id" , cascade = {"persist", "remove"})
*/
protected $magazines;
编辑: 为了更好地解释,这就是关系的样子,其中杂志的顺序很重要,需要保持:
Page_id 1 -> Mag_id:4
-> Mag_id:2
-> Mag_id:1
Page_id 2 -> Mag_id:12
-> Mag_id:1
-> Mag_id:4
等等。
I have 2 entities.
Mag
Page
Which have a ManyToMany relationship. A Page can have many Mags attached to it, but I need to allow the user to adjust the sort order of the Mags.
At the moment ,the order is just based on the id
of the Mag in the Page_Mag
table
So I need a sort field, but then I would need a sort field for each Page->Mag relationship, as there are many Pages which can have any number of Mags attached.
I'm struggling to get my head around the best way of doing this without making a massive mess of my schema!
Any ideas?
My annotations are:
/**
* @ORM\ManyToMany(targetEntity="Page", mappedBy="magazines" , cascade = {"persist", "remove"})
*/
protected $Page;
and
/**
* @ORM\ManyToMany(targetEntity="Magazine", inversedBy="id" , cascade = {"persist", "remove"})
*/
protected $magazines;
EDIT:
To try and explain better , this is what the relationship could be like, where the order of the mags is important to be maintained :
Page_id 1 -> Mag_id:4
-> Mag_id:2
-> Mag_id:1
Page_id 2 -> Mag_id:12
-> Mag_id:1
-> Mag_id:4
etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道您想如何对 Mags 进行排序,但您很可能需要 MagRepository 类,使用 DQL(或 SQL,如果您愿意)按您想要的顺序获取您想要的实体。然后在控制器中使用该存储库。
编辑:
我想我第一次问错了你的问题。
不幸的是,仅使用注释无法在连接表中获取指定杂志中页面位置的字段。您需要一个与您的页面和杂志实体具有一对多关联的连接实体。
I don't know how you would want to sort your Mags but you most likely need a MagRepository Class that uses DQL (or SQL if you like) to fetch the entities you want in the order you want. And then use that repository in your controller.
EDIT:
I think I got your question wrong the first time.
Unfortunately, getting a field in your join table that specifies the position of a Page within a Mag cannot be done using annotations alone. You need a join-entity for that that has one-to-many associations to your Page and Mag entities.
虽然这并不是一个直接的答案,但这就是我解决问题的方法。自定义 Twig 扩展以在视图中执行排序。
然后在
for
中调用它:Although this isn't really a direct answer, this is how I solved my problem. Custom Twig Extension to perform the sort in the view.
this is then called within a
for
: