Skip to content

Commit

Permalink
Merge pull request #180 from gdsmith/popo-fix
Browse files Browse the repository at this point in the history
Fix for missing hydration handling of popo with custom attributes
  • Loading branch information
RemiCollin authored Jul 6, 2017
2 parents 2a6a6d0 + 833ee4a commit 9ae1b12
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/System/Wrappers/PlainObjectWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ protected function hydrate($attributes)

foreach ($properties as $property) {
$name = $property->getName();
$column = $this->getMap()->getColumnNameForAttribute($name);

if ($property->isPublic()) {
$this->entity->$name = $attributes[$name];
$this->entity->$name = $attributes[$column];
} else {
$property->setAccessible(true);
if (isset($attributes[$name])) {
$property->setValue($this->entity, $attributes[$name]);
if (isset($attributes[$column])) {
$property->setValue($this->entity, $attributes[$column]);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/ReflectionMappingTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use TestApp\CustomAttributeRealisator;
use TestApp\Movie;
use TestApp\Realisator;

Expand Down Expand Up @@ -60,4 +61,17 @@ public function we_can_store_a_plain_php_object_with_a_has_many_relationship()
'realisator_id' => $realisator->id,
]);
}

/** @test */
public function we_can_store_and_retrieve_a_plain_php_object_with_a_custom_attribute_mapping()
{
$realisator = new CustomAttributeRealisator('Stanley Kubrick');
$mapper = $this->mapper(CustomAttributeRealisator::class);
$mapper->store($realisator);
$this->seeInDatabase('realisators', [
'name' => 'Stanley Kubrick',
]);
$foundRealisator = $mapper->find($realisator->id);
$this->matches('Stanley Kubrick')->evaluate($foundRealisator->getName());
}
}
26 changes: 26 additions & 0 deletions tests/src/CustomAttributeRealisator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace TestApp;

class CustomAttributeRealisator
{
/**
* @var int
*/
public $id;

/**
* @var string
*/
protected $realisatorName;

public function __construct($name)
{
$this->realisatorName = $name;
}

public function getName()
{
return $this->realisatorName;
}
}
14 changes: 14 additions & 0 deletions tests/src/Maps/CustomAttributeRealisatorMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace TestApp\Maps;

use Analogue\ORM\EntityMap;

class CustomAttributeRealisatorMap extends EntityMap
{
protected $table = 'realisators';

protected $attributes = [
'name' => 'realisatorName',
];
}

0 comments on commit 9ae1b12

Please sign in to comment.