Use DataMapper class in TYPO3 to convert an array into an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function mapProperties(): AnyModel | |
{ | |
$properties = [ | |
'uid' => 123, | |
'pid' => 123, | |
'firstname' => 'Alex', | |
'lastname' => 'Kellner' | |
'email' => 'my@email.org' | |
]; | |
$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' => 'my@email.org' | |
]; | |
$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