如何使用 symfony 对管理面板中自己的列进行排序?
M schema.yml:
News:
columns:
title:
type: string(50)
category_id:
type: integer(4)
relations:
Category:
local: category_id
foreign: category_id
type: one
Category:
columns:
category_name:
type: string(50)
generator:
class: sfDoctrineGenerator
param:
model_class: News
theme: admin
non_verbose_templates: true
with_show: false
singular: ~
plural: ~
route_prefix: news
with_doctrine_route: true
actions_base_class: sfActions
config:
actions: ~
fields: ~
list:
display: [news_id, title, category_name]
filter:
display: [news_id, title, category_id]
form: ~
edit: ~
new: ~
在 news.class 中:
public function getCategoryName()
{
return $this->getCategories()->getCategoryName();
}
这有效,但我无法对此字段进行排序。我可以按 id、标题、category_id 排序,但不能按category_name 排序。如何按此自定义列排序?
M schema.yml:
News:
columns:
title:
type: string(50)
category_id:
type: integer(4)
relations:
Category:
local: category_id
foreign: category_id
type: one
Category:
columns:
category_name:
type: string(50)
generator:
class: sfDoctrineGenerator
param:
model_class: News
theme: admin
non_verbose_templates: true
with_show: false
singular: ~
plural: ~
route_prefix: news
with_doctrine_route: true
actions_base_class: sfActions
config:
actions: ~
fields: ~
list:
display: [news_id, title, category_name]
filter:
display: [news_id, title, category_id]
form: ~
edit: ~
new: ~
In news.class:
public function getCategoryName()
{
return $this->getCategories()->getCategoryName();
}
This works, but I can't sort this field. I can sort by id, title, category_id, but not by category_name. How can I sort by this custom column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些是实现所需结果的步骤。
在generator.yml中定义一个表方法
<前><代码>配置:
行动:~
字段:~
列表:
显示:[新闻 ID、标题、类别名称]
表方法:doSelectJoinCategory
将doSelectJoinCateory添加到NewsTable.class.php
您需要覆盖 actions.class.php 中的排序查询
默认生成器主题将要求您重写 actions.class.php 中的 isValidSortColumn
您将需要覆盖生成器主题以显示排序链接和图标,因为它要求排序字段为真实数据库映射字段。编辑您的 symfony_dir/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/admin/template/templates/_list_th_tabular.php :
将此行更改
为此:
希望对您有帮助
These are the steps to achieve the required result.
Define a table method in your generator.yml
Add doSelectJoinCateory to your NewsTable.class.php
You need to override the sort query in your actions.class.php
The default generator theme will require that you override the isValidSortColumn in actions.class.php
You will need to override the generator theme to display sort link and icons as it requires the sort field to be real database mapped field. edit your symfony_dir/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/admin/template/templates/_list_th_tabular.php :
Change this line
To this :
Hope that helps you
这是因为您正在尝试查看名为
category_name
的列,并且没有getCategory_Name()
方法,解决方案非常简单。显示categoryname
列而不是category_name
。That is because you are trying to view a column named
category_name
and you don't have agetCategory_Name()
method, the solution is very simple. Display thecategoryname
column notcategory_name
.一篇感兴趣的文章解释了如何对所有虚拟列(外部字段)进行排序。
http://sakrawebstudio.blogspot .com/2011/01/sort-by-foreign-key-or-custom-column-in.html
a interested article explaining how to sort all virtual columns(foreign fields).
http://sakrawebstudio.blogspot.com/2011/01/sort-by-foreign-key-or-custom-column-in.html