/**
  * Copies the FAL relations of the given field.
  *
  * @param \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $model
  * @param array $originalRow
  * @param string $originalField
  * @param array $targetConfig
  */
 protected function migrateFalField($model, array $originalRow, $originalField, array $targetConfig)
 {
     if (empty($targetConfig['tableField'])) {
         throw new \RuntimeException(sprintf('tableField config is missing for fal migration for %s field %s', $this->originalTable, $originalField));
     }
     $db = $this->getDatabaseConnection();
     $currentReferences = $db->exec_SELECTgetRows('*', 'sys_file_reference', 'tablenames=' . $db->fullQuoteStr($this->originalTable, 'sys_file_reference') . ' AND fieldname=' . $db->fullQuoteStr($originalField, 'sys_file_reference') . ' AND uid_foreign=' . (int) $originalRow['uid']);
     foreach ($currentReferences as $reference) {
         $existingReferenceCount = $db->exec_SELECTcountRows('uid', 'sys_file_reference', 'tx_czsimplecalimportercal_imported_uid=' . (int) $reference['uid']);
         if ($existingReferenceCount > 0) {
             continue;
         }
         $reference['tablenames'] = $this->targetTable;
         $reference['fieldname'] = $targetConfig['tableField'];
         $reference['uid_foreign'] = (int) $model->getUid();
         $reference['tx_czsimplecalimportercal_imported_uid'] = $reference['uid'];
         unset($reference['uid']);
         $db->exec_INSERTquery('sys_file_reference', $reference);
     }
 }
 /**
  * Creates a sys_file_reference record
  *
  * @param string $table                                  table    the file reference relates to
  * @param string $field                                  field    the file reference relates to
  * @param \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $record  record the file reference relates to
  * @param \TYPO3\CMS\Core\Resource\File                  $file    file the file reference relates to
  * @return array                                                  error log if an error occurs
  */
 protected function addFileReference($table, $field, \TYPO3\CMS\Extbase\DomainObject\AbstractEntity $record, \TYPO3\CMS\Core\Resource\File $file)
 {
     // Make user admin temporarily to circumvent permission issues
     $adminStatus = $GLOBALS['BE_USER']->user['admin'];
     $GLOBALS['BE_USER']->user['admin'] = true;
     // Define content for file reference record
     $data = array();
     $data['sys_file_reference']['NEW1234'] = array('uid_local' => $file->getUid(), 'uid_foreign' => $record->getUid(), 'tablenames' => $table, 'fieldname' => $field, 'pid' => $record->getPid(), 'table_local' => 'sys_file');
     // Create relation to main record
     $data[$table][$record->getUid()] = array($field => 'NEW1234');
     // Use TYPO3 data handler to simulate form submit in backend
     $tce = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
     $tce->start($data, array());
     $tce->process_datamap();
     // Revert original admin status
     $GLOBALS['BE_USER']->user['admin'] = $adminStatus;
     return $tce->errorLog;
 }