How to sort own columns in admin panel with symfony?(如何使用 symfony 在管理面板中对自己的列进行排序?)
问题描述
M schema.yml:
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 中:
In news.class:
public function getCategoryName()
{
  return $this->getCategories()->getCategoryName();
}
这可行,但我无法对该字段进行排序.我可以按 id、title、category_id 排序,但不能按 category_name 排序.如何按此自定义列排序?
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?
推荐答案
这些是达到所需结果的步骤.
These are the steps to achieve the required result.
在你的 generator.yml 中定义一个表格方法
Define a table method in your generator.yml
config:
  actions: ~
  fields:  ~
  list:
    display: [news_id, title, category_name]
    table_method: doSelectJoinCategory
将 doSelectJoinCateory 添加到您的 NewsTable.class.php
Add doSelectJoinCateory to your NewsTable.class.php
class NewsTable extends Doctrine_Table
{
  ...  
  public static function doSelectJoinCategory($query)
  {
    return $query->select('r.*, c.cateogry_name')
      ->leftJoin('r.Category c');
  }
}
你需要覆盖你的actions.class.php中的排序查询
You need to override the sort query in your actions.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]);
}
默认生成器主题将要求您覆盖 actions.class.php 中的 isValidSortColumn
The default generator theme will require that you override the isValidSortColumn in actions.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 :
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 :
改变这一行
<?php if ($field->isReal()): ?>
对此:
<?php if ($field->isReal() || $field->getConfig('sortBy')): ?>
希望对你有帮助
这篇关于如何使用 symfony 在管理面板中对自己的列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 symfony 在管理面板中对自己的列进行排序?
				
        
 
            
        基础教程推荐
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
 - php 7.4 在写入变量中的 Twig 问题 2022-01-01
 - php中的foreach复选框POST 2021-01-01
 - 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
 - 将变量从树枝传递给 js 2022-01-01
 - PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
 - Yii2 - 在运行时设置邮件传输参数 2022-01-01
 - Web 服务器如何处理请求? 2021-01-01
 - 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
 - php中的PDF导出 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				