2017-03-24 29 views
2

在我們的例子中,我們有一個包含使用表B.內部後端模塊,我們導入XML文件導入這些記錄表B.如何在TYPO3中使用DataHandler創建內聯記錄(IRRE)?

所有記錄/對錶格中的數據是可用的IRRE記錄表格中的。 表B的所有數據都可用,但新的uids /標識符除外。

基於https://docs.typo3.org/typo3cms/CoreApiReference/6.2/ApiOverview/Typo3CoreEngine/Database/我必須爲所有新創建的記錄設置標識符NEWxxxx

我一次導入大量記錄。我可以在一個循環中生成這些標識符並一次處理所有記錄,還是必須通過記錄來運行整個數據圖處理記錄?

除了標識符,是否有任何字段我必須在包含IRRE記錄的父記錄上設置?

不涉及翻譯/工作區/其他關係。

感謝您的幫助。

+0

您不必通過記錄創建數據記錄,但是對於批量執行,您必須確保這些'NEW ...'ID是唯一的,不能用於不同的記錄數據。 –

回答

2

DataHandler在TYPO3的是使用下面的數組結構來創建新的或更新現有記錄 - 這是有效直至幷包括TYPO3 CMS 8:

$dataMap = ['<table-name>' => ['<record-uid>' => ['<field-name>' => '<field-value>']] 

現有的記錄使用記錄的uid的整數值字段,例如123,新記錄使用以NEW爲前綴的一些隨機但唯一的標識符,例如, NEWa2b3c4f8uniqid('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) 

準備數據映射

哈瓦仔細看到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', 
    ], 
    ] 
]; 

準備命令映射

// 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 = []; 

執行的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 

如果你需要創建的記錄uid,這可以從內部的DataHandler記錄映射來解決。例如,下面的代碼解析所創建tt_content記錄的新uid

$ttContentId = $dataHandler->substNEWwithIDs['NEW58d5079c8741c8.22627844']; // e.g. 333 

定義的參考字段tt_content.image,它可以包含NEW... IDS以及在例如發生上述的直接現有的整數ID。對於該行爲是在TYPO3所有引用類型是相同的:

  • TCA類型inline,對於所有變體(普通紙,foreign_fieldMM
  • TCA類型select,對於所有變體(普通紙,MM
  • TCA類型group,對於所有變體(普通紙,MM)通過DataHandler確保日誌條目

傳遞數據創建,而且在大多數情況下,修改可以使用TYPO3的歷史/回滾模塊進行恢復。

除此之外,還可以執行批量操作 - DataHandler的調用不僅限於聚合(上述示例中的tt_content記錄)。但是,NEW... ID必須是唯一的,並且在批量執行期間不得重複使用以避免副作用。

轉化到table_a & table_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 ... ], 
    ], 
]; 
相關問題