Symfony 管理生成器中列表视图中的自定义列

发布于 2024-11-26 19:33:23 字数 1527 浏览 2 评论 0原文

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

现在表是一个 client_id 和 sfGuardUser 表上 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 管理生成器显示基于连接的列表视图有什么想法吗?或者,如何从单独的模型中提取?

tl;dr: I need to show data from two different tables in the list view of the Symfony admin generator (preferably via a JOIN statement)

I'm using the sfDoctrineGuardPlugin to manage the users of my app. Each user also has their own profile that they can fill out. The schema (schema.yml) for the profile table looks like this:

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

Right now table is a client_id and a foreign key to the id column on the sfGuardUser table.
(Yes, I know I need an id column in there just for DB accessibility, and more is coming later)

I've created an admin for my profile table using the Symfony admin generator. I would like my list view on the admin to look like this (generator.yml):

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

However, my username column does not work, and I receive a Doctrine_Record_UnkownPropertyException of:

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

I was hoping a custom table_method to populate the data for the list view would help me, but my table method (in lib/model/sfGuardUserProfileTable.class.php) does not seem to help. I followed an example in the Symfony Book, and this is the method:

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

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

  return $q;
}

Any ideas on how to have the Symfony admin generator display a list view based on a join? Or, alternatively, how to pull from a separate model?

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

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

发布评论

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

评论(1

注定孤独终老 2024-12-03 19:33:23

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

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

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

并在我的中添加了一个 getUsername 方法sfGuardUserProfile 模型类

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

感谢您成为我的橡皮鸭,SO!

I have answered my own question!

I changed the join method on my sfGuardUserProfileTable class to something I could better understand (thanks to this helpful link):

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

And threw in a getUsername method in my sfGuardUserProfile model class

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

Thanks for being my rubber duck, SO!

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