Skip to content

[Fix] Unable to delete nested embedded models #1423

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

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 1 addition & 3 deletions src/Jenssegers/Mongodb/Relations/EmbedsMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Arr;
use MongoDB\BSON\ObjectID;

class EmbedsMany extends EmbedsOneOrMany
Expand Down Expand Up @@ -79,8 +78,7 @@ public function performUpdate(Model $model)
// Get the correct foreign key value.
$foreignKey = $this->getForeignKeyValue($model);

// Use array dot notation for better update behavior.
$values = Arr::dot($model->getDirty(), $this->localKey . '.$.');
$values = $this->getUpdateValues($model->getDirty(), $this->localKey . '.$.');

// Update document in database.
$result = $this->getBaseQuery()->where($this->localKey . '.' . $model->getKeyName(), $foreignKey)
Expand Down
4 changes: 1 addition & 3 deletions src/Jenssegers/Mongodb/Relations/EmbedsOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Jenssegers\Mongodb\Relations;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use MongoDB\BSON\ObjectID;

class EmbedsOne extends EmbedsOneOrMany
Expand Down Expand Up @@ -71,8 +70,7 @@ public function performUpdate(Model $model)
return $this->parent->save();
}

// Use array dot notation for better update behavior.
$values = Arr::dot($model->getDirty(), $this->localKey . '.');
$values = $this->getUpdateValues($model->getDirty(), $this->localKey . '.');

$result = $this->getBaseQuery()->update($values);

Expand Down
18 changes: 18 additions & 0 deletions src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,22 @@ protected function getParentKey()
{
return $this->parent->getKey();
}

/**
* Return update values
*
* @param $array
* @param string $prepend
* @return array
*/
public static function getUpdateValues($array, $prepend = '')
{
$results = [];

foreach ($array as $key => $value) {
$results[$prepend.$key] = $value;
}

return $results;
}
}
50 changes: 50 additions & 0 deletions tests/EmbeddedRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,56 @@ public function testNestedMixedEmbeds()
$this->assertEquals('Steve Doe', $user->father->name);
}

public function testNestedEmbedsOneDelete()
{
$user = User::create(['name' => 'John Doe']);
$father = $user->father()->create(['name' => 'Mark Doe']);
$grandfather = $father->father()->create(['name' => 'Steve Doe']);
$greatgrandfather = $grandfather->father()->create(['name' => 'Tom Doe']);

$grandfather->delete();

$this->assertNull($user->father->father);

$user = User::where(['name' => 'John Doe'])->first();
$this->assertNull($user->father->father);
}

public function testNestedEmbedsManyDelete()
{
$user = User::create(['name' => 'John Doe']);
$country = $user->addresses()->create(['country' => 'France']);
$city1 = $country->addresses()->create(['city' => 'Paris']);
$city2 = $country->addresses()->create(['city' => 'Nice']);
$city3 = $country->addresses()->create(['city' => 'Lyon']);

$city2->delete();

$this->assertEquals(2, $user->addresses()->first()->addresses()->count());
$this->assertEquals('Lyon', $country->addresses()->last()->city);

$user = User::where('name', 'John Doe')->first();
$this->assertEquals(2, $user->addresses()->first()->addresses()->count());
$this->assertEquals('Lyon', $country->addresses()->last()->city);
}

public function testNestedMixedEmbedsDelete()
{
$user = User::create(['name' => 'John Doe']);
$father = $user->father()->create(['name' => 'Mark Doe']);
$country1 = $father->addresses()->create(['country' => 'France']);
$country2 = $father->addresses()->create(['country' => 'Belgium']);

$country1->delete();

$this->assertEquals(1, $user->father->addresses()->count());
$this->assertEquals('Belgium', $user->father->addresses()->last()->country);

$user = User::where('name', 'John Doe')->first();
$this->assertEquals(1, $user->father->addresses()->count());
$this->assertEquals('Belgium', $user->father->addresses()->last()->country);
}

public function testDoubleAssociate()
{
$user = User::create(['name' => 'John Doe']);
Expand Down