Skip to content

BelongsToMany sync problem #439

Closed
Closed
@webland2

Description

@webland2

Hi,

Im working with Laravel 4 and mongodb 2.0.4
I have User and Role class and Im trying to use belongsToMany relation with attach, detach and sync methods

User class

public function roles()
{
    return $this->belongsToMany('Role', null, 'user_ids', 'role_ids');
}

Role class

public function users()
{
    return $this->belongsToMany('User', null, 'role_ids', 'user_ids'
}

When I run attach method

$user = User::find($id);
$user->roles()->attach(array($role_id));

mongodb generate one of the query wrong (or not?)

user.update({"_id":{"$id":"54f8d7802228d5e42b000036"}},{"$addToSet":{"role_ids":{"$each":["54f8d7b02228d5e42b000037"]}}},{"multiple":true})

role.update({"_id":["54f8d7b02228d5e42b000037"]},{"$addToSet":{"user_ids":{"$each":["54f8d7802228d5e42b000036"]}}},{"multiple":true})

user collection is updated but role collection stay intact.
It should generate query like this?

role.update({"_id":{"$id":"54f8d7b02228d5e42b000037"}},{"$addToSet":{"user_ids":{"$each":["54f8d7802228d5e42b000036"]}}},{"multiple":true})

This problem is present with both attach and detach methods. Only sync runs correctly. But only if there is one element. If you run sync on multiple elements one of the collections stays always intact because of wrong query.

Am I missing something or there is really a problem with this relation?
Any help would be great. Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions