diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php index 915cc95e2..69918a7b4 100644 --- a/src/Relations/BelongsToMany.php +++ b/src/Relations/BelongsToMany.php @@ -128,7 +128,7 @@ public function sync($ids, $detaching = true) // See issue #256. if ($current instanceof Collection) { - $current = $ids->modelKeys(); + $current = $current->modelKeys(); } $records = $this->formatSyncList($ids); @@ -223,8 +223,9 @@ public function detach($ids = [], $touch = true) // We'll return the numbers of affected rows when we do the deletes. $ids = (array) $ids; - // Detach all ids from the parent model. - $this->parent->pull($this->getRelatedKey(), $ids); + if ($this->parent instanceof \Jenssegers\Mongodb\Eloquent\Model) { + $this->parent->pull($this->getRelatedKey(), $ids); + } // Prepare the query to select all related objects. if (count($ids) > 0) { diff --git a/tests/HybridRelationsTest.php b/tests/HybridRelationsTest.php index 7b4e7cdad..5177347d7 100644 --- a/tests/HybridRelationsTest.php +++ b/tests/HybridRelationsTest.php @@ -9,7 +9,6 @@ class HybridRelationsTest extends TestCase public function setUp(): void { parent::setUp(); - MysqlUser::executeSchema(); MysqlBook::executeSchema(); MysqlRole::executeSchema(); @@ -194,4 +193,50 @@ public function testHybridWith() $this->assertEquals($user->id, $user->books->count()); }); } + + public function testHybridSync() + { + $user = new MysqlUser; + $otherUser = new MysqlUser; + $this->assertInstanceOf(MysqlUser::class, $user); + $this->assertInstanceOf(MySqlConnection::class, $user->getConnection()); + $this->assertInstanceOf(MysqlUser::class, $otherUser); + $this->assertInstanceOf(MySqlConnection::class, $otherUser->getConnection()); + + // Create Mysql Users + $user->name = 'John Doe'; + $user->save(); + $user = MysqlUser::find($user->id); + $otherUser->name = 'Maria Doe'; + $otherUser->save(); + // Create Mongodb Clients + $client = Client::create(['name' => 'Pork Pies Ltd.']); + $otherClient = Client::create(['name' => 'Pork Pies Ltd.']); + + // sync 2 users + $client->usersMysql()->sync([$user->id, $otherUser->id]); + $client = Client::find($client->_id); + $this->assertEquals(2, $client->usersMysql->count()); + // sync 1 user + $client->usersMysql()->sync([$user->id]); + $client = Client::find($client->_id); + $this->assertEquals(1, $client->usersMysql->count()); + // sync 2 users again + $client->usersMysql()->sync([$user->id, $otherUser->id]); + $client = Client::find($client->_id); + $this->assertEquals(2, $client->usersMysql->count()); + + // sync 2 Clients + $user->clients()->sync([$client->_id, $otherClient->_id]); + $user = MysqlUser::find($user->id); + $this->assertEquals(2, $user->clients->count()); + // Sync 1 Client + $user->clients()->sync([$client->_id]); + $user = MysqlUser::find($user->id); + $this->assertEquals(1, $user->clients->count()); + // Sync 2 Clients again + $user->clients()->sync([$client->_id, $otherClient->_id]); + $user = MysqlUser::find($user->id); + $this->assertEquals(2, $user->clients->count()); + } } diff --git a/tests/models/Client.php b/tests/models/Client.php index 2c1388a6c..eace8ddfa 100644 --- a/tests/models/Client.php +++ b/tests/models/Client.php @@ -27,4 +27,9 @@ public function addresses(): HasMany { return $this->hasMany('Address', 'data.client_id', 'data.client_id'); } + + public function usersMysql(): BelongsToMany + { + return $this->belongsToMany('MysqlUser'); + } } diff --git a/tests/models/MysqlUser.php b/tests/models/MysqlUser.php index 8c1393fd5..b6f8fb26b 100644 --- a/tests/models/MysqlUser.php +++ b/tests/models/MysqlUser.php @@ -31,6 +31,11 @@ public function mysqlBooks(): HasMany return $this->hasMany(MysqlBook::class); } + public function clients() + { + return $this->belongsToMany('Client', null, 'mysql_users_id', 'clients'); + } + /** * Check if we need to run the schema. */ @@ -46,5 +51,12 @@ public static function executeSchema(): void $table->timestamps(); }); } + if (! $schema->hasTable('client_mysql_user')) { + Schema::connection('mysql')->create('client_mysql_user', function (Blueprint $table) { + $table->integer('mysql_user_id')->unsigned(); + $table->string('client_id'); + $table->primary(['mysql_user_id', 'client_id']); + }); + } } }