You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was wondering if there was an easy way to map to a destination object where all the properties are in a single associative array?
For example
class Employee
{
private $id;
private $firstName;
private $lastName;
private $birthYear;
public function __construct($id, $firstName, $lastName, $birthYear)
{
$this->id = $id;
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->birthYear = $birthYear;
}
public function getId()
{
return $this->id;
}
// And so on...
}
and MapTo
class EmployeeEntity
{
private $_propDict = [];
public function getId()
{
return $_propDict['id'] ?? null;
}
public function setId($val)
{
$_propDict['id'] = $val;
}
// And so on...
}
The text was updated successfully, but these errors were encountered:
Hi @dingledorf, I don't believe there is an easy/straightforward way. One way to do this is by registering a mapping to an array, and then using that mapping to set the property using mapFrom. One caveat here is that mapping to arrays is not supported in v1, so a workaround is mapping to stdClass and casting to array. As I said, it's not straightforward 🙂
// Register a mapping that dumps everything into a stdClass.$config
->registerMapping(Employee::class, \stdClass::class)
// This is optional, should the propDict use snake cased variable names.
->withNamingConventions(newCamelCaseNamingConvention(), newSnakeCaseNamingConvention());
// Use the previous mapping to set the propDict property.$config
->registerMapping(Employee::class, EmployeeEntity::class)
->forMember(
'_propDict',
Operation::mapFrom(function (Employee$employee, AutoMapper$mapper) {
return (array) $mapper->map($employee, \stdClass::class);
})
);
Hey there,
I was wondering if there was an easy way to map to a destination object where all the properties are in a single associative array?
For example
and MapTo
The text was updated successfully, but these errors were encountered: