我怎样才能像 TYPO3 中的内联字段一样处理 TCA multi select?

How can I handle a TCA multi select like a inline field in TYPO3?

也许我的问题听起来有点奇怪。所以这里是详细信息:

我创建了两个 类 "Position" 和 "Step"。此外,每个步骤都可以包含位置。我的 table 和相关字段如下所示:

CREATE TABLE tx_foxexample_domain_model_step (
    ...
    positions int(11) unsigned DEFAULT '0' NOT NULL,
    ...
);

CREATE TABLE tx_foxexample_domain_model_position (
   ...
   step int(11) unsigned DEFAULT '0' NOT NULL,
   ...
);

我认为这是一个正常的1:n关系,因为每一步都可以存储n个位置,而每个位置只能是一步的一部分。

我的 类 和相关属性如下所示:

class Step extends AbstractEntity
{
    ...
    /**
     * Positions
     *
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Fox\FoxExample\Domain\Model\Position>
     * @cascade remove
     */
     protected $positions = null;
     ...
}

class Position extends AbstractEntity
{
   ...
   /**
    * Stores the relation to step
    *
    * @var \Fox\FoxExample\Domain\Model\Step
    */
    protected $step = null;
   ...
}

该步骤在对象存储中存储了 n 个位置 属性,该位置仅存储了步骤中的相关步骤 属性。

到目前为止,我已经通过内联字段直接为步骤创建了一个或多个位置,我的步骤的 TCA 如下所示:

...
'positions' => array(
    'exclude' => 1,
    'label' => '...',
    'config' => array(
        'type' => 'inline',
         'foreign_table' => 'tx_foxexample_domain_model_position',
         'foreign_field' => 'step',
         'maxitems' => 50,
         'appearance' => array(
             'collapseAll' => 1,
             'levelLinksPosition' => 'top',
             'showSynchronizationLink' => 1,
             'showPossibleLocalizationRecords' => 1,
             'showAllLocalizationLink' => 1
         ),
    ),
),
...

因此位置和步骤关系已正确保存到数据库中,但由于某些原因我已经更改了很多并且不再需要在步骤中创建位置,因为位置已经存在并且用户必须 select 需要的位置。所以我将内联类型更改为多 select 字段:

'positions' => array(
    'exclude' => 1,
    'label' => '...',
    'config' => array(
        'type' => 'select',
         'foreign_table' => 'tx_foxexample_domain_model_position',
         'foreign_table_where' => 'ORDER BY title ASC',
         'minitems' => 0,
         'maxitems' => 50,
    ),
 ),

通过此更改,用户可以 select 每个步骤的相关位置,但是如果我查看数据库 tables 我可以看到我的位置的步骤字段 table 始终为 0,我的步骤 table 的位置字段现在持有位置 uid。

所以我 运行 遇到了一些麻烦,因为我无法为 multi select 字段定义 'foreign_field',所以与位置 table 的关系消失了.此外,我只能添加一个位置,因为只会保存一个位置,而其他 selected 位置将被忽略,因为只会存储一个 uid。

保存前:

保存后:

总而言之,我想保留多 select 字段的内联字段行为。我可以做些什么来保持多 select 字段的内联字段行为?

你可能需要一个MM-Table因为一个步骤可以有多个位置,一个位置可以在多个步骤中分配。我建议引入 table tx_foxexample_step_position_mm:

CREATE TABLE tx_foxexample_step_position_mm (
  uid_local int(11) DEFAULT '0' NOT NULL,
  uid_foreign int(11) DEFAULT '0' NOT NULL,
  sorting int(11) DEFAULT '0' NOT NULL,

  KEY uid_local (uid_local),
  KEY uid_foreign (uid_foreign)
);

您告诉您的 TCA 使用它(其余保持不变):

'positions' => array(
    'config' => array(
         'MM' => 'tx_foxexample_step_position_mm'
    ),
 ),

现在您需要将现有数据迁移到新结构。我建议编写一个 CommandController,用当前关系填充 MM-table。