Sonata admin - “排序依据”相关表中的字段
我有一个产品管理课程。 Product 实体与Category 实体具有多对一的关系,即产品与类别相关联。
在产品的管理“列表”页面中,我需要按每个产品关联的类别名称(按字母顺序)排序。
如果字段位于实体本身上,则设置要排序的默认字段很容易(请参阅 Sonata 管理捆绑顺序< /a> 了解如何执行此操作)。但我不知道如何按相关表中的字段进行排序。
任何帮助表示赞赏。
I have a Product admin class. The Product entity has a many-to-one relationship with a Category entity, i.e. a product is associated with a category.
In the admin "list" page for products, I need to sort by the category name (alphabetically) that each product is associated with.
Setting the default field to sort by is easy if the field is on the entity itself (see Sonata admin bundle order for how to do this). But I cannot figure out how to sort by a field in a related table.
Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是一种解决方法,但它确实有效。您必须添加一个覆盖 createQuery() 方法的连接,而不是指定覆盖 $datagridValues 的默认 sortBy:
It seems a workaround, but it works. You have to add a join overriding createQuery() method, than assign a default sortBy overriding $datagridValues:
假设
name
是您要排序的实体Category
的属性。您可以在ProductAdmin.php
中执行此操作,这样您就可以利用 Sonata 生成的列表标题中的订购链接。
编辑
如果您还想在产品列表中的类别名称上有一个链接以快速编辑
Category
实体,假设您已经创建了CategoryAdmin
类,您应该像这样编写代码:在您的
Category
类中,您应该像这样实现__toString()
方法:Asume
name
is the property of entityCategory
by wich you want to sort. You may do this in youProductAdmin.php
This way you leverage the ordering links in the header of the list, generated by Sonata.
Edit
If you would also like to have a link on the category name in products list to quickly edit the
Category
entity, assuming you have created aCategoryAdmin
class, you should write your code like this:And in your
Category
class you should implement the__toString()
method like this: