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

发布于 2024-11-29 10:09:03 字数 1085 浏览 7 评论 0原文

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

在 news.class 中:

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

这有效,但我无法对此字段进行排序。我可以按 id、标题、category_id 排序,但不能按category_name 排序。如何按此自定义列排序?

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

In news.class:

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

This works, but I can't sort this field. I can sort by id, title, category_id, but not by category_name. How can I sort by this custom column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鯉魚旗 2024-12-06 10:09:03

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

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

    <前><代码>配置:
    行动:~
    字段:~
    列表:
    显示:[新闻 ID、标题、类别名称]
    表方法:doSelectJoinCategory

  2. 将doSelectJoinCateory添加到NewsTable.class.php

    class NewsTable 扩展了 Doctrine_Table
    {
      ...  
      公共静态函数 doSelectJoinCategory($query)
      {
        返回 $query->select('r.*, c.cateogry_name')
          ->leftJoin('r.类别c');
      }
    }
    
  3. 您需要覆盖 actions.class.php 中的排序查询

    class newsActions 扩展了 autoNewsActions
    {
      ...
      受保护函数 addSortQuery($query)
      {
        if (array(null, null) == ($sort = $this->getSort()))
        {
          返回;
        }
    
        if (!in_array(strtolower($sort[1]), array('asc', 'desc')))
        {
          $sort[1] = 'asc';
        }
    
        开关($排序[0]){
          案例“类别名称”:
          $sort[0] = 'c.category_name';
          休息;
        }
    
      $query->addOrderBy($sort[0] . ' ' . $sort[1]);
    }
    
  4. 默认生成器主题将要求您重写 actions.class.php 中的 isValidSortColumn

    受保护函数 isValidSortColumn($column)
    {
      return Doctrine_Core::getTable('付款')->hasColumn($column) || $column == '类别名称';
    }
    
  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')): ?>

希望对您有帮助

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

一抹微笑 2024-12-06 10:09:03

这是因为您正在尝试查看名为 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();
}

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();
}
呆萌少年 2024-12-06 10:09:03

一篇感兴趣的文章解释了如何对所有虚拟列(外部字段)进行排序。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文