管理生成器:为什么我不能显示文章的状态,而不是 state_id?

发布于 2024-09-27 01:29:32 字数 1218 浏览 1 评论 0原文

我正在 Symfony 中做一个博客引擎。这是我的架构的一部分:

alt text

Content:
  connection: doctrine
  tableName: ec_content
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: true
      primary: true
      autoincrement: true
(...)
  relations:
    Author:
      local: author_id
      foreign: id
      type: one
    State:
      local: state_id
      foreign: id
      type: one
    Type:
      local: type_id
      foreign: id
      type: one
(...)

在管理页面中,我想显示文章的类型,但仅限 symfony显示 type_id,这是为什么?

编辑:这是我的generator.yml:我还没有对其进行太多修改。

generator:
  class: sfDoctrineGenerator
  param:
    model_class:           Content
    theme:                 admin
    non_verbose_templates: true
    with_show:             false
    singular:              ~
    plural:                ~
    route_prefix:          content_Brouillons
    with_doctrine_route:   true
    actions_base_class:    sfActions

    config:
      actions: ~
      fields:  ~
      list:
        title: Brouillons
        display: [Type, State, title, link]
      filter:  ~
      form:    ~
      edit:    ~
      new:     ~

I'm doing a blog engine in Symfony. Here's part of my schema :

alt text

Content:
  connection: doctrine
  tableName: ec_content
  columns:
    id:
      type: integer(4)
      fixed: false
      unsigned: true
      primary: true
      autoincrement: true
(...)
  relations:
    Author:
      local: author_id
      foreign: id
      type: one
    State:
      local: state_id
      foreign: id
      type: one
    Type:
      local: type_id
      foreign: id
      type: one
(...)

In the administration pages, I want to display the type of the articles, but symfony only shows the type_id, why is that ?

EDIT: here's my generator.yml : I haven't modified it much yet.

generator:
  class: sfDoctrineGenerator
  param:
    model_class:           Content
    theme:                 admin
    non_verbose_templates: true
    with_show:             false
    singular:              ~
    plural:                ~
    route_prefix:          content_Brouillons
    with_doctrine_route:   true
    actions_base_class:    sfActions

    config:
      actions: ~
      fields:  ~
      list:
        title: Brouillons
        display: [Type, State, title, link]
      filter:  ~
      form:    ~
      edit:    ~
      new:     ~

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

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

发布评论

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

评论(1

浮生未歇 2024-10-04 01:29:32

好的。

在您的generator.yml中,在display行上,Symfony(通过Doctrine)将在您的模型类中查找与您想要显示的每个字段相对应的字段名称。如果字段名称不存在,它将查找相应的 getFieldName() 方法并调用该方法。

在您的示例中,您将 Type 作为字段名称,它将调用 getType() - 这将获取关系。默认情况下,Doctrine 假设当您想要要将模型转换为字符串(例如,用于在列表中显示),您需要使用主键 - 在您的情况下,即 ID 值。

为了解决这个问题,请在您的 Doctrine lib/model/doctrine/EcType.class.php 文件中添加一个 __toString() 方法,如下所示:

class EcType extends BaseEcType
{
  public function __toString()
  {
    return $this->type;
  }
}

您应该看到 'type ' 字段显示在您的管理员生成的列表中。

OK.

In your generator.yml, on the display line, Symfony (via Doctrine) will look for a field name in your model class that corresponds to each field you want to display. If the field name doesn't exist, it will then look for a corresponding getFieldName() method and call that.

In your example, you have Type as a field name, which will be calling getType() - this will fetch the relation in. By default, Doctrine assumes that when you want to convert a model to a string (eg for display in your list), you want to use the primary key - in your case, the ID value.

To overcome this, add a __toString() method as follows, to your Doctrine lib/model/doctrine/EcType.class.php file:

class EcType extends BaseEcType
{
  public function __toString()
  {
    return $this->type;
  }
}

and you should see the 'type' field being displayed in your admin generated list.

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