Skip to content

Instantly share code, notes, and snippets.

@einpraegsam
Last active November 3, 2023 16:15
Show Gist options
  • Save einpraegsam/679156ccd9fb1cb94af5f901519d89fc to your computer and use it in GitHub Desktop.
Save einpraegsam/679156ccd9fb1cb94af5f901519d89fc to your computer and use it in GitHub Desktop.
Use DataMapper class in TYPO3 to convert an array into an object
public function mapProperties(): AnyModel
{
$properties = [
'uid' => 123,
'pid' => 123,
'firstname' => 'Alex',
'lastname' => 'Kellner'
'email' => '[email protected]'
];
$dataMapper = $this->objectManager->get(TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper::class);
$anyModels = $dataMapper->map(AnyModel::class, [$properties]);
return $anyModels[0];
}
public function mapPropertiesOld(): AnyModel
{
$properties = [
'uid' => 123,
'pid' => 123,
'firstname' => 'Alex',
'lastname' => 'Kellner'
'email' => '[email protected]'
];
$anyModel = $this->objectManager->get(AnyModel::class);
$anyModel->setUid($properties['uid');
$anyModel->setPid($properties['pid');
$anyModel->setFirstname($properties['firstname');
$anyModel->setLastname($properties['lastname');
$anyModel->setEmail($properties['email');
return $anyModel;
}
public function mapManyRecords(): array
{
$tableName = 'tx_ext_domain_model_table';
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
$result = $queryBuilder->select('*')->from($tableName)->setMaxResults(10000)->execute();
$dataMapper = $this->objectManager->get(DataMapper::class);
return $dataMapper->map(Table::class, $result->fetchAll());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment