1

Im working with Laravel 4 and mongodb 2.0.4 module 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_ds":{"$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

nabizan
  • 3,185
  • 5
  • 26
  • 38
  • Looks buggy. Would you possibly be prepared to submit a comparison example where one relational side is `belongsToMany()` and the other side is `hasMany()`. The lack of `ObjectId` typing is disturbing. As is the list conversion handling. I would be intrigued to see if my mentioned test case produces a similar or different result. – Neil Lunn Mar 10 '15 at 10:28
  • I have a similiar structure. When using $user->roles I get back an array of ids. I would like to have the objects instead. Do I have a wrong configuration or is this normal behavior? – Sven Mäurer Nov 18 '16 at 11:03

1 Answers1

1

Replace

$user->roles()->attach(array($role_id));

With

$user->roles()->attach($role_id);

If your parameter is not array, you have to use attach method. sync method only accept array as parameter. Here is a good explanation about them. Hope it will be useful for you.

Community
  • 1
  • 1
Arkar Aung
  • 3,554
  • 1
  • 16
  • 25
  • this is wrong. I have created couple unit test to prove it https://github.com/jenssegers/laravel-mongodb/commit/8d06ac5342b00705e2dd7a78ea84368a3c7ed697 the proble is within library itself. my question states only one attaching role_id but this is not a point, its just an example of wrong behaviour – nabizan Mar 18 '15 at 08:54
  • What's wrong ?? Check it https://github.com/jenssegers/laravel-mongodb/blob/master/src/Jenssegers/Mongodb/Relations/BelongsToMany.php#L155. I just pointed out based on your code. – Arkar Aung Mar 18 '15 at 09:06
  • @nabizan you find any solution of the problem with sync()? – Inzmam ul Hassan Jan 10 '17 at 08:15
  • @InzmamGujjar already solved and merged to master. see https://github.com/jenssegers/laravel-mongodb/issues/439 – nabizan Jan 11 '17 at 08:52