【问题标题】:Custom columns in list view in Symfony admin generatorSymfony 管理生成器列表视图中的自定义列
【发布时间】:2011-07-27 21:13:06
【问题描述】:

tl;dr: 我需要在 Symfony 管理生成器的列表视图中显示来自两个不同表的数据(最好通过 JOIN 语句)

我正在使用sfDoctrineGuardPlugin 来管理我的应用程序的用户。每个用户也有自己的个人资料,他们可以填写。配置文件表的架构 (schema.yml) 如下所示:

sfGuardUserProfile:
  connection: doctrine
  tableName: sf_guard_user_profile
  columns:
    sf_guard_user_id: integer(8)
    client_id: string(128)
  relations:
    sfGuardUser:
      local: sf_guard_user_id
      foreign: id
      foreignType: one
      onDelete: CASCADE

现在表是 sfGuardUser 表上 id 列的 client_id 和外键。 (是的,我知道我需要一个 id 列,只是为了数据库可访问性,以后还会有更多)

我使用 Symfony 管理员生成器为我的个人资料表创建了一个管理员。我希望我的管理员列表视图看起来像这样 (generator.yml):

config:
  ...
  list:
    title:   Clients
    display: [client_id, sf_guard_user_id, username]
    table_method: retrieveProfileList

但是,我的用户名列不起作用,我收到了 Doctrine_Record_UnkownPropertyException 的:

Unknown record property / related component "username" on "sfGuardUserProfile"

我希望为列表视图填充数据的自定义 table_method 对我有帮助,但我的表格方法(在 lib/model/sfGuardUserProfileTable.class.php 中)似乎没有帮助。我按照Symfony Book中的一个例子,就是这个方法:

public static function retrieveProfileList(Doctrine_Query $q)
{
  $rootAlias = $q->getRootAlias();

  $q->leftJoin($rootAlias . '.sfGuardUser id');

  return $q;
}

关于如何让 Symfony 管理生成器显示基于连接的列表视图有什么想法吗?或者,如何从单独的模型中提取?

【问题讨论】:

    标签: doctrine symfony-1.4 admin-generator


    【解决方案1】:

    我已经回答了我自己的问题!

    我将 sfGuardUserProfileTable 类的 join 方法更改为我可以更好理解的方法(感谢 this 有用的链接):

    public static function retrieveProfileList(Doctrine_Query $q)
    {
      return $q->select('r.*, u.username')
              ->leftJoin('r.sfGuardUser u');
    }
    

    并在我的sfGuardUserProfile 模型类中加入了getUsername 方法

      public function getUsername()
      {
          return $this->sfGuardUser->username;
      }
    

    感谢你成为我的橡皮鸭,所以!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      相关资源
      最近更新 更多