簡體   English   中英

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

[英]How to sort own columns in admin panel with symfony?

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:     ~

在新聞中。class:

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

這有效,但我無法對該字段進行排序。 我可以按 id、title、category_id 排序,但不能按 category_name 排序。 如何按此自定義列排序?

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

  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. 您需要在您的操作中覆蓋排序查詢。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。class.php

     protected function isValidSortColumn($column) { return Doctrine_Core::getTable('Payment')->hasColumn($column) || $column == 'cateogry_name'; }
  5. 您將需要覆蓋生成器主題以顯示排序鏈接和圖標,因為它要求排序字段是真實的數據庫映射字段。 編輯您的 symfony_dir/lib/plugins/sfDoctrinePlugin/data/generator/sfDoctrineModule/admin/template/templates/_list_th_tabular.php:

更改此行

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

對此:

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

希望對你有幫助

那是因為您正在嘗試查看名為category_name的列並且您沒有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();
}

一篇感興趣的文章,解釋如何對所有虛擬列(外域)進行排序。

http://sakrawebstudio.blogspot.com/2011/01/sort-by-foreign-key-or-custom-column-in.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM