简体   繁体   English

如何处理TYPO3中的内联字段等TCA多选?

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

Maybe my question sounds a little bit weird. 也许我的问题听起来有点奇怪。 So here are the details: 因此,这是详细信息:

I have created the two classes "Position" and "Step". 我创建了两个类“Position”和“Step”。 Furthermore every step can contain positions. 此外,每个步骤都可以包含位置。 My tables and the relevant fields looks like the following: 我的表和相关字段如下所示:

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,
   ...
);

I think it is a normal 1:n relation, because each step can store n-positions and every position could be part of only one step. 我认为这是一个正常的1:n关系,因为每个步骤都可以存储n个位置,每个位置只能是一个步骤的一部分。

My classes and the relevant properties looks like the following: 我的类和相关属性如下所示:

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;
   ...
}

The step stores n-positions in a object storage property and the position just stores the relevant step in a step property. 步骤将n个位置存储在对象存储属性中,而该位置仅将相关步骤存储在step属性中。

So far I have created a position or some positions for a step directly inline via the inline field, the TCA for my step was like the following: 到目前为止,我通过内联字段直接内联创建了一个位置或一些位置,我的步骤的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
         ),
    ),
),
...

So the positions and step relations were correctly saved into database, but for some reasons I have changed a lot and it is not necessary anymore to create the positions inside the steps because the positions already exists and a user have to select the positions which are necessary. 因此,位置和步骤关系已正确保存到数据库中,但是由于某些原因,我进行了很多更改,并且不再需要在步骤内部创建位置,因为位置已经存在并且用户必须选择必要的位置。 So I changed the inline type to a multi select field: 所以我将内联类型更改为多选字段:

'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,
    ),
 ),

With this changes a user can select the relevant positions for each step, but if I take a look at the database tables I can see that the step field of my positions table is always 0 and the positions field of my step table holds the position uid now. 进行此更改后,用户可以选择每个步骤的相关位置,但是如果查看数据库表,我可以看到我的位置表的step字段始终为0,而​​我的步骤表的positions字段包含位置uid现在。

So I run into some trouble because I can't define a 'foreign_field' for the multi select field, so the relation to the position table is gone. 所以我遇到了一些麻烦,因为我无法为多选字段定义'foreign_field',因此与位置表的关系消失了。 Furthermore I can only add one position because only one position will be saved and the other selected positions will be ignored because only one uid will be stored. 此外,我只能添加一个位置,因为只保存一个位置,其他选定的位置将被忽略,因为只存储一个uid。

Before save: 保存之前: 在此输入图像描述

After save: 保存后: 在此输入图像描述

In summary I want to keep the inline field behaviour for the multi select field. 总之,我想保留多选择字段的内联字段行为。 What can I do to keep the inline field behaviour for a multi select field? 如何保持多选字段的内联字段行为?

You probably need a MM-Table because a step can have multiple positions and a position can be assigned in multiple steps. 您可能需要MM表,因为一个步骤可以有多个位置,并且可以多个步骤分配一个位置。 I'd suggest to introduce a table tx_foxexample_step_position_mm : 我建议介绍一个表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)
);

You tell your TCA to use it (rest stays the same): 您告诉您的TCA使用它(其余保持不变):

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

Now you need to migrate the existing data to the new structure. 现在,您需要将现有数据迁移到新结构。 I suggest writing a CommandController that populates the MM-table with the current relations. 我建议编写一个CommandController,用当前关系填充MM表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM