2011-08-11 28 views
1

中號的schema.yml:如何使用symfony在管理面板中對自己的列進行排序?

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:

public function getCategoryName() 
{ 
    return $this->getCategories()->getCategoryName(); 
} 

這工作,但我不能這個字段排序。我可以按id,title,category_id排序,但不能按category_name排序。我如何根據此自定義列進行排序?

回答

5

這些是實現所需結果的步驟。

  1. 定義您generator.yml表方法

    config: 
        actions: ~ 
        fields: ~ 
        list: 
        display: [news_id, title, category_name] 
        table_method: doSelectJoinCategory 
    
  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'); 
        } 
    } 
    
  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]); 
    } 
    
  4. 默認生成的主題將要求您覆蓋isValidSortColumn中的actions.class.php

    protected function isValidSortColumn($column) 
    { 
        return Doctrine_Core::getTable(‘Payment’)->hasColumn($column) || $column == ‘cateogry_name’; 
    } 
    
  5. 你需要重寫生成主題,以顯示排序鏈接和圖標,因爲它需要排序字段是真正的數據庫映射字段編輯symfony_dir/lib目錄/插件/ sfDoctrinePlugin /數據/發電機/ sfDoctrineModule /管理/模板/模板/ _list_th_tabular.php:

改變這一行

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

要這樣:

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

希望能幫到你

+0

非常非常非常感謝你! :) –

+0

在您的'generator.yml'的'fields'部分,您可以定義一個字段應該被視爲真實:'category_name:{is_real:true}',因此您不必編輯模板。另外,'isValidSortColumn()'可以使用對父函數的引用,所以不需要使用實際的表名(可以改變超時)。 –

1

那是因爲你正在試圖查看名爲的列並且您沒有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(); 
} 
相關問題