Skip to content

Many to many, can't get related objects #75

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

Closed
rcwsr opened this issue Nov 27, 2013 · 15 comments
Closed

Many to many, can't get related objects #75

rcwsr opened this issue Nov 27, 2013 · 15 comments

Comments

@rcwsr
Copy link

rcwsr commented Nov 27, 2013

I'm implementing a many to many relationship between two models, User and Club. The documentation says "The belongsToMany relation will not use a pivot "table", but will push id's to a related_ids attribute instead.".

However, I've set up belongsToMany on both models, and when attaching or syncing, a pivot table is created named, for example. user_club. I can even specify the name of the pivot-table in my models, as well as pass extra attributes to store in the pivot-table when syncing or or attaching. When creating the relationship, nothing is stored Is this expected behavior?

If it is, then I'm having trouble getting related items. For example, I'm trying to display all the users that belong to a club with $users = $club->users();. But in a var_dump, I can't see anything related to users. Am I doing something wrong?

@jenssegers
Copy link
Contributor

Are all your models extending the included model class?

@rcwsr
Copy link
Author

rcwsr commented Nov 27, 2013

Yes, I've tried using the alias and use Jenssegers\Mongodb\Model as Eloquent;. My Club class:

use Jenssegers\Mongodb\Model as Eloquent;
class Club extends Eloquent {

    protected $collection = 'clubs';

    public function users()
    {
        return $this->belongsToMany('User', 'memberships');
    }
}

And my User class is set up in a similar manner.

The relationship is successfully recorded in the memberships pivot-table.

@rcwsr
Copy link
Author

rcwsr commented Nov 27, 2013

I've just looked at my original post, I started writing something then forgot about it. Where I wrote:

"When creating the relationship, nothing is stored Is this expected behavior?"

I meant, nothing is stored in either model collection, but the relationship is stored in the pivot table.

@jenssegers
Copy link
Contributor

No it should not create a pivot table. It will add a 'membership_ids' to your user document and a 'user_ids' to your membership document.

@rcwsr
Copy link
Author

rcwsr commented Nov 27, 2013

The pivot table is created whether or not I specify a pivot table name, or pass the relationship any data upon creation. Have I just accidentally created a feature?!

A document from the pivot-collection in question, status is an extra field.

{
  "_id" : ObjectId("529619f9fa4634b4038b456a"),
  "user_id" : "5293a000fa463492038b456b",
  "club_id" : "52951ff2fa4634d9038b4567",
  "status" : "active"
}

@jenssegers
Copy link
Contributor

Are you using the dev-master version or the latest tag?

@rcwsr
Copy link
Author

rcwsr commented Nov 27, 2013

"jenssegers/mongodb": "*"

@jenssegers
Copy link
Contributor

And you are using the latest version? BelongsToMany support has only been added 7 days ago.

@rcwsr
Copy link
Author

rcwsr commented Nov 27, 2013

Argh I think I might have an older version! Should I be using dev-master instead of *? I only set this laravel project up at the weekend, I assumed it would download the latest.

@jenssegers
Copy link
Contributor

Run composer update to be sure :)

@rcwsr
Copy link
Author

rcwsr commented Nov 27, 2013

Thanks, that's sorted it. One question, is there a way to name the field in the model collection that holds the referenced ids?

@jenssegers
Copy link
Contributor

I should look into that to be sure. Will let you know asap.

@jenssegers
Copy link
Contributor

You can, I just added a test for it in 34ea812

Just keep in mind that the second parameter for the belongsToMany methods does not actually do anything, since it defines the name of the pivot table.

For example:

public function groups()
{
    return $this->belongsToMany('Group', 'does_not_matter', 'users', 'groups');
}

jenssegers added a commit that referenced this issue Nov 29, 2013
@jenssegers
Copy link
Contributor

I just added some documentation on this in the readme.

@rcwsr
Copy link
Author

rcwsr commented Dec 1, 2013

Thanks for this.

mnphpexpert added a commit to mnphpexpert/laravel-mongodb that referenced this issue Sep 2, 2024
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