如何在TYPO3中使用DataHandler创建内联记录(IRRE)?

Mai*_*ser 2 typo3 datahandler typo3-6.2.x

在我们的例子中,我们有一个表A,其中包含使用表B的IRRE记录。在后端模块内部,我们导入XML文件以为表B导入这些记录。

表A的所有记录/数据均可用。表B的所有数据均可用,但新的uid /标识符除外。

基于https://docs.typo3.org/typo3cms/CoreApiReference/6.2/ApiOverview/Typo3CoreEngine/Database/,我必须NEWxxxx为所有新创建的记录设置标识符。

我要一次导入大量记录。我是否可以在循环中生成这些标识符并一次处理所有记录,还是必须逐条记录运行整个datamap-handling记录?

除了标识符,我还必须在包含IRRE记录的父记录上设置任何字段吗?

不涉及翻译/工作空间/其他关系。

谢谢你的帮助。

Oli*_*der 6

TYPO3中的DataHandler使用以下数组结构来创建新记录或更新现有记录-这在TYPO3 CMS 8及更高版本中有效:

$dataMap = ['<table-name>' => ['<record-uid>' => ['<field-name>' => '<field-value>']]
Run Code Online (Sandbox Code Playgroud)

现有记录使用记录uid字段的整数值,例如123,新记录使用一些随机但唯一的标识符,这些标识符带有前缀NEW,例如NEWa2b3c4f8,通过uniqid('NEW', true)-使用TYPO3 CMS 7 创建的标识符StringUtility::getUniqueId('NEW')也可以用于此。

通用示例

假定将创建以下记录:*表中的新内容元素tt_content * sys_file_reference字段的表的两个新内联文件引用tt_content.image + sys_file用uid 123 引用现有sys_file记录+ 用uid 引用现有记录234

$ttContentId = 'NEW58d5079c8741c8.22627844'; // uniqid('NEW', true)
$fileRefId1st = 'NEW58d506f3cd0c41.59344142'; // uniqid('NEW', true)
$fileRefId2nd = 'NEW58d50714c12260.92562338'; // uniqid('NEW', true) 
Run Code Online (Sandbox Code Playgroud)

准备数据图

哈瓦一个近距离观察tt_content.image,这其实是定义(新)内嵌引用,用逗号分隔定义的新记录或现有记录的值- 。这可能是NEWabc,NEWdef123,234,345NEWabc,123,NEWdef混合新的和现有的记录引用。

$dataMap = [
  'tt_content' => [
    'NEW58d5079c8741c8.22627844' => [
      'title' => 'My new content element',
      'bodytext' => 'Look at the following images...',
      'CType' => 'textpic',
      // $fileRefId1st & $fileRefId2nd, the sorting order is defined by this as well
      'image' => 'NEW58d506f3cd0c41.59344142,NEW58d50714c12260.92562338',
    ],
  ],
  'sys_file_reference' => [
    'NEW58d506f3cd0c41.59344142' => [
      'uid_local' => 123,
      'title' => 'Image #123',
    ],
    'NEW58d50714c12260.92562338' => [
      'uid_local' => 234,
      'title' => 'Image #234',
    ],
  ]
];
Run Code Online (Sandbox Code Playgroud)

准备命令图

// the command-maps is similar to the data-map to copy, localize, move records
// however, it's not required in this scenario and thus stays empty
$commandMap = [];
Run Code Online (Sandbox Code Playgroud)

执行DataHandler

$dataHandler = new \TYPO3\CMS\Core\DataHandling\DataHandler();
$dataHandler->start($dataMap, $commandMap);
$dataHandler->process_datamap();
// $dataHandler->process_cmdmap(); // if $commandMap should be processed as well
Run Code Online (Sandbox Code Playgroud)

如果您需要uid创建的记录,则可以从内部DataHandler记录映射中解决。例如,以下代码解析新uid创建的tt_content记录:

$ttContentId = $dataHandler->substNEWwithIDs['NEW58d5079c8741c8.22627844']; // e.g. 333
Run Code Online (Sandbox Code Playgroud)

笔记

在上面的示例中直接为field定义引用,该字段tt_content.image可以包含NEW...ID以及现有的整数ID。对于TYPO3中的所有引用类型,的行为相同:

  • TCA类型inline,适用于所有变体(“ foreign_field,” MM
  • TCA类型select,适用于所有变体(纯MM
  • TCA类型group,适用于所有变体(纯MM

传递数据DataHandler可确保创建日志条目,并且在大多数情况下,可以使用TYPO3的历史记录/回滚模块还原修改。

除此之外,还可以执行大规模操作-的调用DataHandler不仅限于汇总(tt_content上面示例中的记录)。但是,这些NEW...ID必须是唯一的,并且在批量执行期间不得重复使用,以免产生副作用。

转换为table_atable_b方案

将其转换为初始问题的table_atable_b场景,$dataMap可能类似于以下内容。当然,您必须确定要table_b绑定到哪些引用table_a

$dataMap = [
  // existing records of table_a, thus using the real ids
  'table_a' => [
    '11' => [ 'reference_field' => 'NEW_b_1,NEW_b_2' ],
    '22' => [ 'reference_field' => 'NEW_b_3,NEW_b_4' ],
    '33' => [ 'reference_field' => 'NEW_b_5,NEW_b_6' ],
  ],
  // new records to be references for table_b, thus using NEW... ids
  'table_b' => [
    'NEW_b_1' => [ ... field values of this particular table_b record ... ],
    'NEW_b_2' => [ ... field values of this particular table_b record ... ],
    'NEW_b_3' => [ ... field values of this particular table_b record ... ],
    'NEW_b_4' => [ ... field values of this particular table_b record ... ],
    'NEW_b_5' => [ ... field values of this particular table_b record ... ],
    'NEW_b_6' => [ ... field values of this particular table_b record ... ],
  ],
];
Run Code Online (Sandbox Code Playgroud)