【问题标题】:TYPO3 9 Extbase: edit m:n relation from both parent and child elements in backend formTYPO3 9 Extbase:从后端表单中的父元素和子元素编辑 m:n 关系
【发布时间】:2019-07-04 08:36:48
【问题描述】:

在我的扩展中,我有两个模型:父模型是博士课程的研究小组子模型申请轮次 (包括开始日期和结束日期)。 在中间表中,我存储了参与轮次申请的研究组的两者之间的关系。申请轮次可以与参与两个平行轮次的某些组重叠。这就是为什么我需要将其设为 m:n 关系的原因。我使用official documentation 完成了所有工作。

虽然我可以在研究组表单中选择多个申请轮次,但我还想让申请轮次的后端表单显示所有可供选择的组的列表。

我发现 information on inline fields 显示了双向使用 - 但我正在努力将其转化为我的扩展程序的情况。

注意:我不想要内联编辑,实际上只需要两边的 selectCheckBox 列表。

Groups 域模型包含以下属性:

/**
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Myvendor\Researchgroups\Domain\Model\ApplicationRounds>
 */
protected $applicationRounds = NULL;

application_rounds INT(10)保存在数据库中

Groups TCA 包含以下定义:

$GLOBALS['TCA']['tx_researchgroups_domain_model_groups']['columns'] = [
    'application_rounds' => [
        'label' => '' . $locLang . 'groups.recruitingSelection',
        'config' => [
            'type' => 'select',
            'renderType' => 'selectCheckBox',
            'foreign_table' => 'tx_researchgroups_domain_model_applicationrounds',
            'foreign_table_where' => 'ORDER BY tx_researchgroups_domain_model_applicationrounds.date_open DESC',
            'MM' => 'tx_researchgroups_groups_applicationrounds_mm',
            'eval' => 'int',
        ],
    ],
]

如何向ApplicationRounds 模型添加相应的选择列表?我是否还必须在那里添加一个属性,持久保存在数据库中——即使认为这会是多余的?

【问题讨论】:

    标签: foreign-keys relationship extbase typo3-9.x


    【解决方案1】:

    你需要设置mm-opposite-field:

    如果你想让一个MM关系也可以从关系的外部(双向)编辑,你需要将外部的MM_opposite_field设置为本地的字段名称。

    [由原始提问者编辑] 请参阅 next answer 以获取基于此答案的完整代码

    【讨论】:

    • 谢谢!根据这个tutorial on typo3tiger,感谢您的提示,我还必须在子模型数据库表中添加一个字段researchgroups INT(11)。是对的吗?或者有可能避免这种情况吗?我能够显示预先选择了正确组的列表,但是在点击保存时,我收到了错误消息,指出字段 researchgroups 不存在。
    • 当然你需要一个“反向链接”字段。否则你不能从那里编辑。
    • 好的,谢谢。现在它可以工作了——尽管从数据库的角度来看,我想这不是必需的。
    • 一般来说,您不需要这个“锚”字段,因为 mm 记录包含所有信息。锚字段可能有历史原因,因为 TYPO3 以前将 uid 列表存储在此关系字段中(并且没有 mm 记录)。稍后您仍然需要一个占位符来编辑后端的关系。这些字段是 int 的,因为存储了关系的数量(因此该信息并不完全可靠)。当然,如果您对同一种记录有多种不同用途的关系,则当然需要这些字段(请查看 ext:news。可能与文件有多种关系)
    【解决方案2】:

    这是我按照Bernd’s answer 中的提示添加的完整工作代码:

    ApplicationRoundsTCA(子模型)

    $GLOBALS['TCA']['tx_researchgroups_domain_model_applicationrounds']['columns'] = [
        'researchgroups' => [
            'label' => '' . $locLang . 'researchgroups_applicationrounds.recruitingGl',
            'config' => [
                'type' => 'select',
                'renderType' => 'selectCheckBox',
                'foreign_table' => 'tx_researchgroups_domain_model_groups',
                'foreign_table_where' => ' AND tx_researchgroups_domain_model_groups.hidden = 0 ORDER BY tx_researchgroups_domain_model_groups.last_name ASC',
                'MM' => 'tx_researchgroups_groups_applicationrounds_mm',
                'MM_opposite_field' => 'last_name',
                'eval' => 'int',
            ],
        ],
    ]
    

    (别忘了将researchgroups添加到$GLOBALS['TCA']['tx_researchgroups_domain_model_applicationrounds']['types'][1]

    并在子模型的数据库表中添加一个字段: researchgroups INT(11) NOT NULL DEFAULT '0',

    虽然我没有将researchgroups 添加到域模型中。

    【讨论】:

      猜你喜欢
      • 2018-12-04
      • 1970-01-01
      • 2020-10-05
      • 2020-11-19
      • 1970-01-01
      • 2023-04-01
      • 2021-03-21
      • 2014-09-21
      • 2020-12-04
      相关资源
      最近更新 更多