【问题标题】:How to realize inheritance in Typo3 6.2 Extension?Typo3 6.2 Extension中如何实现继承?
【发布时间】:2016-09-27 13:17:57
【问题描述】:

我的目标是能够:

  1. 在后端创建Expertise条目(已完成)
  2. 在后端创建SubExpertise 条目
    • (与 Expertise 相同的道具,但 他们属于一个或多个Expertise)
  3. 在后端创建AdditionalInfoTitles 条目
    • (它们可以属于一个或多个ExpertiseSubExpertise
    • 我希望在创建新条目时能够从所有 ExpertiseSubExpertise 中选择对象

现在我只能在所有Expertise-Entries 之间进行选择:

这就是为什么我从那时起就考虑继承的原因,SubExpertise 将与 Expertise 属于同一类型,因此会自动显示在 Expertise 列表中的 AdditionalInfoTitles 条目中。但这只是我的理论,而我对typo3 TCA 和其他我缺乏的知识有点卡在现实中......

在我的扩展构建器中,我做了以下操作(不要介意 subExpertises 属性)
然后我将expertise 添加到Overrides 文件夹中,因为我正在尝试使用subexpertise 扩展它:

<?php
if (!defined('TYPO3_MODE')) {
        die ('Access denied.');
}

$temporaryColumns = array (
        'expertise' => array(
        'exclude' => 1,
        'label' => 'LLL:EXT:appoints/Resources/Private/Language/locallang_db.xlf:tx_appoints_domain_model_subexpertise.expertise',
        'config' => array(
            'type' => 'select',
            'foreign_table' => 'tx_appoints_domain_model_subexpertise',
            'MM' => 'tx_appoints_subexpertise_expertise_mm',
            'size' => 10,
            'autoSizeMax' => 30,
            'maxitems' => 9999,
            'multiple' => 0,
            'wizards' => array(
                '_PADDING' => 1,
                '_VERTICAL' => 1,
                'edit' => array(
                    'module' => array(
                        'name' => 'wizard_edit',
                    ),
                    'type' => 'popup',
                    'title' => 'Edit',
                    'icon' => 'edit2.gif',
                    'popup_onlyOpenIfSelected' => 1,
                    'JSopenParams' => 'height=350,width=580,status=0,menubar=0,scrollbars=1',
                    ),
                'add' => Array(
                    'module' => array(
                        'name' => 'wizard_add',
                    ),
                    'type' => 'script',
                    'title' => 'Create new',
                    'icon' => 'add.gif',
                    'params' => array(
                        'table' => 'tx_appoints_domain_model_expertise',
                        'pid' => '###CURRENT_PID###',
                        'setValue' => 'prepend'
                    ),
                ),
            ),
        ),
    ),
);

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
        'tx_appoints_domain_model_expertise',
        $temporaryColumns
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
        'tx_appoints_domain_model_expertise',
        'expertise'
);

但我不认为我会朝着正确的方向前进 - 因为我认为这样我就不能在后端单独添加SubExpertiseExpertise - 我的对象已经有同样的问题,扩展fe_user 因为在创建它们时我通常必须通过一个新用户,然后设置扩展类型 - 但这样我就没有扩展 fe_user 的不同实体的单独列表。

【问题讨论】:

  • ExpertiseSubExpertise 之间除了后者有一个或多个父母这一事实之外,还有其他区别吗?如果不是,我会将它们视为一回事,只需创建 Expertise 对象。 AdditionalInfoTitles 的同样问题。
  • 是的,Expertise 和 SubExpertise 是一样的,区别在于 SubExpertise 可以属于一个或多个 Expertise。 AdditionalInfoTitle 完全不同,它的属性与此问题无关 - 除了我希望能够为其选择 Expertise 和 SubExpertise 条目的 Expertise-Property。

标签: typo3 backend extend typo3-6.2.x


【解决方案1】:

我会在很大程度上摆脱专业知识和子专业知识之间的分离。根据您的描述,子专家不能将另一个子专家作为其父项,因此您可以调整选择字段,使其仅列出具有空父字段的专家。 通过消除差异,消除了在 AdditionalInfoTitles 中选择 (Sub)Expertise 的问题;它只是一种相同类型的对象。

如果您需要在 BE 表单中进行区分,有很多选项可以调整列出项目的标签,使用您自己的函数来构建列表甚至自定义表单元素。

在 Extbase 中,您可以简单地在存储库中编写一些函数来获取 Expertise、SubExpertise 或两者。

【讨论】:

  • 谢谢 - 今天我也开始考虑是否拥有一个实体不会让事情变得更简单 - 我会尝试一下,一旦它起作用,我会将你的答案标记为已接受! :)
【解决方案2】:

如果实体 SubExpertise 在您的域模型中没有意义,那么 Jigal 的答案非常适合您的场景。如果它确实有意义,您可以在 Extbase 中使用单表继承来实现。

class Expertise extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
    // all common properties
}

class SubExpertise extends Expertise
{
    /**
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\[YourVendorName]\Appoints\Domain\Model\Expertise>
     */
    protected $expertises;

    public function __construct()
    {
      $this->expertises = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
    }

    public function getExpertises() {}
    public function setExpertises($expertises) {}
}

然后您必须通过 TypoScript 定义映射规则,因为 ExpertiseSubExpertise 将存储在同一个表中 tx_appoints_domain_model_subexpertise

您将在Extbase book 中找到有关单表继承的更多详细信息。

【讨论】:

  • 也感谢您提供替代方案 - 当我终于有时间时,我将深入阅读这两种方法,并根据哪种方法更适合我执行,我将选择接受的答案:)跨度>
猜你喜欢
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 2018-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
相关资源
最近更新 更多