1
votes

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?

3

3 Answers

5
votes

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

1
votes

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();
}
0
votes

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