如何使用symfony对管理面板中的自己的列进行排序?

Moi*_*ade 1 php doctrine symfony1 symfony-1.4

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:     ~
Run Code Online (Sandbox Code Playgroud)

在news.class中:

public function getCategoryName()
{
  return $this->getCategories()->getCategoryName();
}
Run Code Online (Sandbox Code Playgroud)

这有效,但我不能对这个字段进行排序.我可以按id,title,category_id排序,但不能按category_name排序.如何按此自定义列排序?

小智 5

这些是实现所需结果的步骤.

  1. 在generator.yml中定义一个表方法

    config:
      actions: ~
      fields:  ~
      list:
        display: [news_id, title, category_name]
        table_method: doSelectJoinCategory
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将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');
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 您需要在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]);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 默认生成器主题将要求您覆盖actions.class.php中的isValidSortColumn

    protected function isValidSortColumn($column)
    {
      return Doctrine_Core::getTable(‘Payment’)->hasColumn($column) || $column == ‘cateogry_name’;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  5. 您需要覆盖生成器主题以显示排序链接和图标,因为它需要排序字段为真实数据库映射字段.编辑你的symfony_dir/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/admin/template/templates/_list_th_tabular.php:

改变这一行

<?php if ($field->isReal()): ?>
Run Code Online (Sandbox Code Playgroud)

对此:

<?php if ($field->isReal() || $field->getConfig('sortBy')): ?>
Run Code Online (Sandbox Code Playgroud)

希望对你有所帮助