Skip to content

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

Closed
@rcwsr

Description

@rcwsr

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?

Activity

jenssegers

jenssegers commented on Nov 27, 2013

@jenssegers
Contributor

Are all your models extending the included model class?

rcwsr

rcwsr commented on Nov 27, 2013

@rcwsr
Author

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

rcwsr commented on Nov 27, 2013

@rcwsr
Author

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

jenssegers commented on Nov 27, 2013

@jenssegers
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

rcwsr commented on Nov 27, 2013

@rcwsr
Author

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

jenssegers commented on Nov 27, 2013

@jenssegers
Contributor

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

rcwsr

rcwsr commented on Nov 27, 2013

@rcwsr
Author

"jenssegers/mongodb": "*"

jenssegers

jenssegers commented on Nov 27, 2013

@jenssegers
Contributor

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

rcwsr

rcwsr commented on Nov 27, 2013

@rcwsr
Author

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

jenssegers commented on Nov 27, 2013

@jenssegers
Contributor

Run composer update to be sure :)

rcwsr

rcwsr commented on Nov 27, 2013

@rcwsr
Author

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

jenssegers commented on Nov 27, 2013

@jenssegers
Contributor

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

jenssegers

jenssegers commented on Nov 29, 2013

@jenssegers
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');
}
added a commit that references this issue on Nov 29, 2013
jenssegers

jenssegers commented on Nov 29, 2013

@jenssegers
Contributor

I just added some documentation on this in the readme.

4 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @jenssegers@rcwsr

        Issue actions

          Many to many, can't get related objects · Issue #75 · mongodb/laravel-mongodb