How to sort own columns in admin panel with symfony?

前端 未结 3 1359
后悔当初
后悔当初 2021-01-23 23:21

M schema.yml:

News:
  columns:
    title:
      type: string(50)
    category_id:
      type: integer(4)
  relations:
    Category:
      local: category_id
             


        
相关标签:
3条回答
  • 2021-01-23 23:29

    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();
    }
    
    0 讨论(0)
  • 2021-01-23 23:29

    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

    0 讨论(0)
  • 2021-01-23 23:30

    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

    0 讨论(0)
提交回复
热议问题