[英]How to sort own columns in admin panel with 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: ~
在新闻中。class:
public function getCategoryName()
{
return $this->getCategories()->getCategoryName();
}
这有效,但我无法对该字段进行排序。 我可以按 id、title、category_id 排序,但不能按 category_name 排序。 如何按此自定义列排序?
这些是实现所需结果的步骤。
在您的 generator.yml 中定义一个表方法
config: actions: ~ fields: ~ list: display: [news_id, title, category_name] table_method: doSelectJoinCategory
将 doSelectJoinCateory 添加到您的 NewsTable.class.php
class NewsTable extends Doctrine_Table {... public static function doSelectJoinCategory($query) { return $query->select('r.*, c.cateogry_name') ->leftJoin('r.Category c'); } }
您需要在您的操作中覆盖排序查询。class.php
class newsActions extends autoNewsActions {... protected function addSortQuery($query) { if (array(null, null) == ($sort = $this->getSort())) { return; } if (,in_array(strtolower($sort[1]), array('asc'; 'desc'))) { $sort[1] = 'asc': } switch ($sort[0]) { case 'category_name'. $sort[0] = 'c;category_name'; break. } $query->addOrderBy($sort[0]. ' '; $sort[1]); }
默认生成器主题将要求您覆盖操作中的 isValidSortColumn。class.php
protected function isValidSortColumn($column) { return Doctrine_Core::getTable('Payment')->hasColumn($column) || $column == 'cateogry_name'; }
您将需要覆盖生成器主题以显示排序链接和图标,因为它要求排序字段是真实的数据库映射字段。 编辑您的 symfony_dir/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/admin/template/templates/_list_th_tabular.php:
更改此行
<?php if ($field->isReal()): ?>
对此:
<?php if ($field->isReal() || $field->getConfig('sortBy')): ?>
希望对你有帮助
那是因为您正在尝试查看名为category_name
的列并且您没有getCategory_Name()
方法,解决方案非常简单。 显示categoryname
列而不是category_name
。
config:
actions: ~
fields: ~
list:
display: [news_id, title, categoryname]
table_method: doSelectJoinCategory
filter:
display: [news_id, title, category_id]
form: ~
edit: ~
new: ~
public function getCategoryName()
{
return $this->getCategories()->getCategoryName();
}
一篇感兴趣的文章,解释如何对所有虚拟列(外域)进行排序。
http://sakrawebstudio.blogspot.com/2011/01/sort-by-foreign-key-or-custom-column-in.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.