2011-08-11 7 views
1

Mのschema.ymlの:news.classで管理パネルで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:  ~ 

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

これは動作しますが、私はこのフィールドを並べ替えることはできません。 id、title、category_idでソートできますが、category_nameではソートできません。このカスタム列でどのようにソートできますか?

答えて

5

これは、必要な結果を得るための手順です。

  1. あなたはあなたの中に並べ替えクエリをオーバーライドする必要があなたのNewsTable.class.phpに

    class NewsTable extends Doctrine_Table 
    { 
        ... 
        public static function doSelectJoinCategory($query) 
        { 
        return $query->select('r.*, c.cateogry_name') 
         ->leftJoin('r.Category c'); 
        } 
    } 
    
  2. をdoSelectJoinCateoryを追加

    config: 
        actions: ~ 
        fields: ~ 
        list: 
        display: [news_id, title, category_name] 
        table_method: doSelectJoinCategory 
    
  3. あなたにgenerator.ymlでテーブルのメソッドを定義します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. デフォルト・ジェネレータのテーマは、あなたがそれがためにソートフィールドを必要とするあなたは、ソートのリンクやアイコンを表示するには、発電機のテーマをオーバーライドする必要がありますactions.class.phpを

    protected function isValidSortColumn($column) 
    { 
        return Doctrine_Core::getTable(‘Payment’)->hasColumn($column) || $column == ‘cateogry_name’; 
    } 
    
  5. でisValidSortColumnをオーバーライドすることが必要になります実際のデータベースマップフィールド。あなたのsymfony_dir/libに/ plugins /にするsfDoctrinePlugin /データ/ジェネレータ/ sfDoctrineModule /管理/テンプレート/テンプレート/ _list_th_tabular.phpを編集:

変更このラインこれに

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

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

あなたを助ける希望

+0

あなたのために非常に非常に非常に感謝! :) –

+0

'generator.yml'の' fields'セクションでは、フィールドをreal: '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(); 
} 
関連する問題