TYPO3 extbase&IRRE:使用'foreign_selector'添加现有记录

Urs*_*Urs 3 typo3 extbase typo3-6.2.x

我使用包含1:1和1:n关系的extbase扩展构建器"kickstart"扩展.它会自动将字段类型设置为"内联",并在后端显示一个漂亮的IRRE UI.

但默认情况下,无法选择现有记录,只需创建新记录即可.

在此输入图像描述

我找到了关于如何使用'foreign_selector'实现这一点的各种解释,但所有这些都非常粗略.该功能本身应该正常工作,请参阅https://forge.typo3.org/issues/43239

有人可以引导我通过这个或指向TER中的工作示例吗?一旦我开始工作,我就可以从示例中创建一个分步教程.

PS该字段的TCA配置由extension_builder以下生成:

'myfield' => array(
    'exclude' => 1,
    'label' => 'LLL:EXT:myextension/Resources/Private/Language/locallang_db.xlf:tx_myextension_domain_model_myitem.myfield',
    'config' => array(
        'type' => 'inline',
        'foreign_table' => 'tx_myextension_domain_model_myfield',
        'foreign_field' => 'myitem',
        'maxitems'      => 9999,
        'appearance' => array(
            'collapseAll' => 0,
            'levelLinksPosition' => 'top',
            'showSynchronizationLink' => 1,
            'showPossibleLocalizationRecords' => 1,
            'showAllLocalizationLink' => 1
        ),
    ),
),
Run Code Online (Sandbox Code Playgroud)

lor*_*enz 8

主要问题是类型1的IRRE关系:n的工作方式如下:子记录保存其父级的uid.所以你的表tx_myext_domain_model_city保存你的(虚构的)tx_myext_domain_model_address的UID.

因此,使用默认配置,您将无法多次选择城市,因为它只能拥有一个父项.

因此,您需要为此字段使用关系表.此表需要包含地址(uid_address)和城市(uid_city)的uid字段:

CREATE TABLE tx_irreforeignselectordemo_address_city_mm (

    uid int(11) NOT NULL auto_increment,
    pid int(11) DEFAULT '0' NOT NULL,
    uid_address int(11) unsigned DEFAULT '0' NOT NULL,
    uid_city int(11) unsigned DEFAULT '0' NOT NULL,
    sorting int(11) unsigned DEFAULT '0' NOT NULL,

    PRIMARY KEY (uid),
    KEY parent (pid)
);
Run Code Online (Sandbox Code Playgroud)

它需要为这些字段配置TCA(而表本身可以隐藏):

return array(
    'ctrl'      => array(
        'title'     => 'Relation table',
        'hideTable' => TRUE,
        'sortby'    => 'sorting',
    ),
    'columns'   => array(
        'uid_address' => Array(
            'label'  => 'Address',
            'config' => Array(
                'type'          => 'select',
                'foreign_table' => 'tx_irreforeignselectordemo_domain_model_address',
                'size'          => 1,
                'minitems'      => 0,
                'maxitems'      => 1,
            ),
        ),
        'uid_city'   => Array(
            'label'  => 'City',
            'config' => Array(
                'type'                => 'select',
                'foreign_table'       => 'tx_irreforeignselectordemo_domain_model_city',
                'foreign_table_where' => ' AND sys_language_uid IN (0,-1)',
                'size'                => 1,
                'minitems'            => 0,
                'maxitems'            => 1,
            ),
        ),
    ),
    'types'     => array(
        '0' => array('showitem' => 'uid_address,uid_city')
    ),
    'palettes'  => array()
);
Run Code Online (Sandbox Code Playgroud)

然后,您可以配置地址的TCA以使其成为IRRE字段:

'type' => 'inline',
'foreign_table' => 'tx_yourext_address_city_mm',
'foreign_field' => 'uid_address',
'foreign_label' => 'uid_city',
'foreign_selector' => 'uid_city',
'foreign_unique' => 'uid_city',
'foreign_sortby' => 'sorting',
Run Code Online (Sandbox Code Playgroud)

请注意,foreign_unique告诉TYPO3城市只能选择一次.

而且你需要从另一方(来自你的城市TCA)定义关系:

    'addresses' => array(
        'exclude' => 1,
        'label'   => 'Addresses',
        'config'  => array(
            'type' => 'inline',
            'foreign_table' => 'tx_irreforeignselectordemo_address_city_mm',
            'foreign_field' => 'uid_city',
            'foreign_label' => 'uid_address',
        ),
    ),
Run Code Online (Sandbox Code Playgroud)

配置完成后,您就可以在后端使用它.

由于这是一种非标准的MM关系,因此默认情况下Extbase无法处理它.但是我们可以将它与TYPO3 6中引入的sys_file_reference表进行比较.因此,我们使用属性"address"和"city"为CityRelation构建一个Extbase模型,并将此模型映射到我们的mm表:

config.tx_extbase.persistence.classes {
    Visol\Irreforeignselectordemo\Domain\Model\CityRelation {
        mapping {
            tableName = tx_irreforeignselectordemo_address_city_mm
            columns {
                uid_address.mapOnProperty = address
                uid_city.mapOnProperty = city
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在在我们的地址模型中,我们将城市(或您允许多个选择的城市)定义为CityRelation类型的ObjectStorage:

/**
 * Cities
 *
 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Visol\Irreforeignselectordemo\Domain\Model\CityRelation>
 */
protected $cities = NULL;
Run Code Online (Sandbox Code Playgroud)

我们现在有一个属性"城市",其中包含对所有选定城市的引用.您可以遍历它们并使用它们:

<f:for each="{address.cities}" as="cityRelation">
  <li>{cityRelation.city.name}</li>
</f:for>
Run Code Online (Sandbox Code Playgroud)

由于我找不到一个这样的一体化演示,并对该主题感兴趣,我创建了一个演示扩展,完成我刚才描述的 - 基于Core和两个处理该主题的扩展:https:/ /github.com/lorenzulrich/irreforeignselectordemo

无论如何,解决方案是m:n方法(因为1:n因上述原因而无效)所以我决定使用"城市"而不是"城市".虽然这可能对选择城市没有意义(如你的帖子所建议的),但对其他机会可能有意义.随意用"city"替换"cities"并将内联配置中的maxItems设置为1 - 那么你就有了1:n的种类.