Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for alias mapping bug #258

Merged
merged 2 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,14 +1011,10 @@ public function convertToPHPValues($entityName, array $data)
{
$phpData = [];
$fields = $entityName::fields();
$fieldAliasMappings = $this->entityManager()->fieldAliasMappings();
$platform = $this->connection()->getDatabasePlatform();
$data = $this->resolver()->dataWithOutFieldAliasMappings($data);
$entityData = array_intersect_key($data, $fields);
foreach ($data as $field => $value) {
if ($fieldAlias = array_search($field, $fieldAliasMappings)) {
$field = $fieldAlias;
}

// Field is in the Entity definitions
if (isset($entityData[$field])) {
$typeHandler = Type::getType($fields[$field]['type']);
Expand Down
17 changes: 17 additions & 0 deletions lib/Query/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,23 @@ public function dataWithFieldAliasMappings(array $data)
return $fieldMappings;
}

/**
* Taken given aliased field name/value inputs and map them to their non-aliased names
*/
public function dataWithOutFieldAliasMappings(array $data)
{
// have to call fields() otherwise fieldAliasMappings() would return null on the first entity
$this->mapper->entityManager()->fields();
$fieldAliasMappings = $this->mapper->entityManager()->fieldAliasMappings();
foreach ($fieldAliasMappings as $field => $aliasedField) {
if (array_key_exists($aliasedField, $data)) {
$data[$field] = $data[$aliasedField];
unset($data[$aliasedField]);
}
}
return $data;
}

/**
* Execute provided query and return result
*
Expand Down
2 changes: 2 additions & 0 deletions tests/Entity/Legacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public static function fields()
'name' => ['type' => 'string', 'required' => true, 'column' => self::getNameFieldColumnName()],
'number' => ['type' => 'integer', 'required' => true, 'column' => self::getNumberFieldColumnName()],
'date_created' => ['type' => 'datetime', 'value' => new \DateTime(), 'column' => self::getDateCreatedColumnName()],
'array' => ['type' => 'array', 'required' => false],
'arrayAliased'=> ['type' => 'array', 'required' => false, 'column' => 'array_aliased']
];
}

Expand Down
21 changes: 21 additions & 0 deletions tests/FieldAlias.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,25 @@ public function testLegacyRelations(Legacy $legacy)
$this->assertInstanceOf('Spot\Relation\HasMany', $legacy->polymorphic_comments);
$this->assertEquals(count($legacy->polymorphic_comments), 1);
}

public function testFieldAliasMapping() {
$testId = 2545;
$testArray = ['testKey' => 'testValue'];

$legacy = new Legacy();
$legacy->id = $testId;
$legacy->name = 'Something Here';
$legacy->number = 5;
$legacy->array = $testArray;
$legacy->arrayAliased = $testArray;

$mapper = test_spot_mapper('SpotTest\Entity\Legacy');
$mapper->save($legacy);

unset($legacy);
$legacy = $mapper->get($testId);

$this->assertEquals($testArray, $legacy->array);
$this->assertEquals($testArray, $legacy->arrayAliased);
}
}