Skip to content
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

Sync method not removing ids in belongsToMany relationship #72

Closed
kmmathis opened this issue Nov 27, 2013 · 6 comments
Closed

Sync method not removing ids in belongsToMany relationship #72

kmmathis opened this issue Nov 27, 2013 · 6 comments

Comments

@kmmathis
Copy link

I have User and Role models, with each one having a belongsToMany relationship with the other, i.e. User belongsToMany Role and Role belongsToMany User.

Consider the following example:

$user = User::find("5294f9d695463439088b4569");
$user->roles()->sync(array("52953c599546345b048b456a", "52953c599546345b048b456b"));
// the above code puts the Role id's on the User document, as expected.
// it also puts the User id on the Role documents, as expected.

// later on...

$user->roles()->sync(array());
// that line removes the Role ids from the User document
// but it does not remove the User id from the Role documents

Shouldn't the User id be removed from the Role documents when the sync method passes an empty array? Or is that not what sync is meant to do?

@jenssegers
Copy link
Contributor

Maybe @dyeh could pitch in? I will take a look at it as well.

@kmmathis
Copy link
Author

I also tested out the attach / detach methods and got the same result as using the sync method

$user = User::find("5294f9d695463439088b4569");
$user->roles()->attach("52953c599546345b048b456a");
// the above code puts the Role id on the User document, as expected.
// it also puts the User id on the Role document, as expected.

// later on...

$user->roles()->detach("52953c599546345b048b456a");
// that line removes the Role id from the User document
// but it does not remove the User id from the Role document

@ghost
Copy link

ghost commented Nov 27, 2013

Will look into this, I did test this and it seemed to work ok.

@jenssegers
Copy link
Contributor

If you add this it will fail:

        // Detach clients from user
        $user->clients()->sync(array());

        // Get the new user model
        $user = User::where('name', '=', 'Jane Doe')->first();
        $client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();

        // Assert they are not attached
        $this->assertFalse(in_array($client->_id, $user->client_ids));
        $this->assertFalse(in_array($user->_id, $client->user_ids));
        $this->assertCount(0, $user->clients);
        $this->assertCount(1, $client->users);

@ghost
Copy link

ghost commented Nov 27, 2013

Pull request 57c5bed should fix this

@kmmathis
Copy link
Author

Works incredibly well. Beaming with joy. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants