Skip to content
Open
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
7 changes: 4 additions & 3 deletions src/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
47 changes: 46 additions & 1 deletion tests/HybridRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class HybridRelationsTest extends TestCase
public function setUp(): void
{
parent::setUp();

MysqlUser::executeSchema();
MysqlBook::executeSchema();
MysqlRole::executeSchema();
Expand Down Expand Up @@ -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());
}
}
5 changes: 5 additions & 0 deletions tests/models/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
12 changes: 12 additions & 0 deletions tests/models/MysqlUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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']);
});
}
}
}