Skip to content

Commit

Permalink
fix - fix errors when using plain objects
Browse files Browse the repository at this point in the history
- When using plain PHP object relations were broken
  • Loading branch information
adrorocker committed Feb 11, 2019
1 parent 8833e55 commit 401b1de
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/Commands/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ protected function insert()
unset($attributes[$keyName]);
}

if (isset($attributes['attributes'])) {
unset($attributes['attributes']);
}

$id = $this->query->insertGetId($attributes, $keyName);

$aggregate->setEntityAttribute($keyName, $id);
Expand Down
7 changes: 3 additions & 4 deletions src/EntityMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1572,10 +1572,7 @@ public function getMorphClass(): string
*/
public function newCollection(array $entities = []): EntityCollection
{
$collection = new EntityCollection($entities);
$keyName = $this->getAttributeNameForColumn($this->getKeyName());

return $collection->keyBy($keyName);
return new EntityCollection($entities);
}

/**
Expand Down Expand Up @@ -1745,6 +1742,7 @@ public function getAttributeNamesFromColumns($array)
return $newArray;
}
if ($this->camelCaseHydratation) {
$newArray = [];
foreach ($array as $key => $value) {
$attributeName = camel_case($key);
$newArray[$attributeName] = $value;
Expand Down Expand Up @@ -1796,6 +1794,7 @@ public function getColumnNamesFromAttributes($array)
return $newArray;
}
if ($this->camelCaseHydratation) {
$newArray = [];
foreach ($array as $key => $value) {
$attributeName = snake_case($key);
$newArray[$attributeName] = $value;
Expand Down
4 changes: 2 additions & 2 deletions src/System/Wrappers/ObjectWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected function dehydrate($entity): array
if (isset($properties[$attributesName])) {
$properties[$attributesName] = $this->entityMap->getColumnNamesFromAttributes($properties[$attributesName]);
} else {
$properties = $this->entityMap->getColumnNamesFromAttributes($properties);
$properties[$attributesName] = $this->entityMap->getColumnNamesFromAttributes($properties);
}

$this->properties = $properties;
Expand All @@ -191,7 +191,7 @@ protected function hydrate()
if (isset($properties[$attributesName])) {
$properties[$attributesName] = $this->entityMap->getAttributeNamesFromColumns($properties[$attributesName]);
} else {
$properties = $this->entityMap->getAttributeNamesFromColumns($properties);
$properties[$attributesName] = $this->entityMap->getAttributeNamesFromColumns($properties);
}

// In some case, attributes will miss some properties, so we'll just complete the hydration
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ public function we_can_access_mapped_columns()
$user = $mapper->find($id);
$this->assertEquals('123456', $user->rememberToken);
$this->assertEquals('adro', $user->identity->fname);
$user->rememberToken = '1234567';
$mapper->store($user);
$user = null;
$user = $mapper->find($id);
$this->assertEquals('1234567', $user->rememberToken);
}

/** @test */
Expand All @@ -113,6 +118,15 @@ public function we_can_access_camel_case_properities()
$movie = null;
$movie = $mapper->find($id);
$this->assertEquals('analogue is awesome', $movie->getSomeText());
$movie->setSomeText('analogue is awesome!!');
$mapper->store($movie);
$movie = null;
$movie2 = $mapper->find($id);
$this->assertEquals('analogue is awesome!!', $movie2->getSomeText());
$this->seeInDatabase('movies', [
'title' => 'Analogue Tutorial',
'some_text' => 'analogue is awesome!!',
]);
}

/** @test */
Expand Down
3 changes: 1 addition & 2 deletions tests/src/Maps/MovieMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ class MovieMap extends EntityMap
'id',
'title',
'realisator',
'some_text',
];

protected $camelCaseHydratation = true;

protected $arrayName = null;

public function realisator(Movie $movie)
{
return $this->belongsTo($movie, Realisator::class);
Expand Down

0 comments on commit 401b1de

Please sign in to comment.