How to sort own columns in admin panel with symfony?

两盒软妹~` 提交于 2019-12-31 04:10:48

问题


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?


回答1:


These are the steps to achieve the required result.

  1. Define a table method in your generator.yml

    config:
      actions: ~
      fields:  ~
      list:
        display: [news_id, title, category_name]
        table_method: doSelectJoinCategory
    
  2. 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');
      }
    }
    
  3. 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]);
    }
    
  4. 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’;
    }
    
  5. 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

<?php if ($field->isReal()): ?>

To this :

<?php if ($field->isReal() || $field->getConfig('sortBy')): ?>

Hope that helps you




回答2:


That is because you are trying to view a column named category_name and you don't have a getCategory_Name() method, the solution is very simple. Display the categoryname column not 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();
}



回答3:


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



来源:https://stackoverflow.com/questions/7023247/how-to-sort-own-columns-in-admin-panel-with-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!