Skip to content

Commit

Permalink
Use DataHandler commands to create and update data for General Handler
Browse files Browse the repository at this point in the history
Instead of direct sql inserts and updates.
  • Loading branch information
tmotyl committed Jul 26, 2018
1 parent dd32611 commit b596fd5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
45 changes: 45 additions & 0 deletions Classes/TcaDataGenerator/TableHandler/AbstractTableHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\DataHandling\DataHandler;
use TYPO3\CMS\Core\Utility\StringUtility;
use TYPO3\CMS\Styleguide\TcaDataGenerator\RecordFinder;

/**
Expand Down Expand Up @@ -74,6 +75,50 @@ protected function generateTranslatedRecords(string $tableName, $fieldValues)
}
}

/**
* Creates new record using DataHandler
*
* @param string $tableName
* @param array $fieldValues
* @return int
*/
protected function insertRecord(string $tableName, array $fieldValues)
{
$UidPlaceholder = StringUtility::getUniqueId('NEW');

$dataMap = [
$tableName => [
$UidPlaceholder => $fieldValues
]
];
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
$dataHandler->start($dataMap, []);
$dataHandler->process_datamap();
return (int)$dataHandler->substNEWwithIDs[$UidPlaceholder];
}


/**
* Updates a record using datahandler
*
* @param string $tableName
* @param int $uid
* @param array $fieldValues
* @return int
*/
protected function updateRecord(string $tableName, $uid, array $fieldValues)
{
$dataMap = [
$tableName => [
$uid => $fieldValues
]
];
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
$dataHandler->start($dataMap, []);
$dataHandler->process_datamap();
return (int)$uid;
}

/**
* @param string $tableName
* @param int $uid
Expand Down
13 changes: 4 additions & 9 deletions Classes/TcaDataGenerator/TableHandler/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function match(string $tableName): bool
* Adds rows
*
* @param string $tableName
* @return string
*/
public function handle(string $tableName)
{
Expand All @@ -53,15 +52,11 @@ public function handle(string $tableName)
$fieldValues = [
'pid' => $recordFinder->findPidOfMainTableRecord($tableName),
];
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
$connection->insert($tableName, $fieldValues);
$fieldValues['uid'] = $connection->lastInsertId($tableName);
$uid = $this->insertRecord($tableName, $fieldValues);
$fieldValues['uid'] = $uid;

$fieldValues = $recordData->generate($tableName, $fieldValues);
$connection->update(
$tableName,
$fieldValues,
[ 'uid' => $fieldValues['uid'] ]
);
$this->updateRecord($tableName, $uid, $fieldValues);

$this->generateTranslatedRecords($tableName, $fieldValues);
}
Expand Down

0 comments on commit b596fd5

Please sign in to comment.